关于下面这段程序的疑问主要是,对于TestB来说无论有几个TestB的对象,其TestA对象只有一个(因为它是static),那么对于运行的后的线程呢?他们是否每条线程中有包含一个TestA的副本呢?(因为我想不通,为什么5个线程都能知道自己是在什么地方挂起,又需要从什么地方开始被唤醒,所以我觉得是不是每个线程都有一个TestA对象的副本,以记录是在什么地方被挂起的)
class TestA{
public synchronized void myTest(){
try{
while(true){
wait();
System.out.println(Thread.currentThread() + "");
}
}catch(InterruptedException e){
System.out.println("End Interrupted");
}
}

public synchronized void myNotifyAll(){
notifyAll();
}
}class TestB implements Runnable{
static TestA testA = new TestA();
public void run(){
testA.myTest();
}
}public class Test1{
public static void main(String[] args)throws Exception{
ExecutorService exes = Executors.newCachedThreadPool();
for(int i = 0; i < 5; i++){
exes.execute(new TestB());
}
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run(){
System.out.println("\nnotifyAll() ");
TestB.testA.myNotifyAll();
}
} ,400 ,400);

TimeUnit.SECONDS.sleep(2);
timer.cancel();
exes.shutdownNow();
}
}

解决方案 »

  1.   

    5个线程的run方法本身并没有被挂起,如果run方法在testA.myTest();前面还有其它语句,而且是多CPU情况下它们就是并发/并行执行.
    只有在执行到testA.myTest()时才产竞争,都在等待testA的锁.这个锁是唯一的.
      

  2.   

    答:每一个线程对象(如:5个线程对象TestB),共用同一个引用:static TestA testA .
    不是每个线程都有一个TestA对象的副本.
    每一个线程对象拥有自己的栈与堆及PC计数器,正是它们来完成:自己是在什么地方挂起,又需要从什么地方开始被唤醒,这个被称为:线程运行的资源条件(这个作为线程运行所需要的资源条件,是由:线程的start()来完成分配的,只有资源条件得到了,才会自动去调用run())
      

  3.   

    你看下线程的优先级。。在未被设置(默认优先级)的时候如3楼所说。。完全由start()自己控制的