Thread对象也会在堆中分配空间吗?

Graveworm 2010-06-05 08:38:46
在学java concurrent部分的知识,看到下面这样一个程序有点困惑:

public class TestSync implements Runnable {
Timer timer = new Timer();
public static void main(String[] args) {
TestSync test = new TestSync();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
public void run(){
timer.add(Thread.currentThread().getName());
}
}

class Timer{
private static int num = 0;
public synchronized void add(String name){

num ++;
try {Thread.sleep(1);}
catch (InterruptedException e) {}
System.out.println(name+", 你是第"+num+"个使用timer的线程");

}
}

对这段程序的内存分配有疑问的地方是:两个Thread对象究竟在堆中有没有分配内存呢?t1和t2这两个引用是否也是在main的栈中存在呢?
在线等,期待大家的帮助,谢谢
...全文
186 3 打赏 收藏 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
24K純帥 2010-06-05
  • 打赏
  • 举报
回复
喔,学习。。
dracularking 2010-06-05
  • 打赏
  • 举报
回复
Thread也是对象 有线程专属堆区(thread-specific heaps)概念,也有共享堆区概念(shared heap)
cileen 2010-06-05
  • 打赏
  • 举报
回复
为什么不分配内存

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧