关于下面这段程序的疑问主要是,对于TestB来说无论有几个TestB的对象,其TestA对象只有一个(因为它是static),那么对于运行的后的线程呢?他们是否每条线程中有包含一个TestA的副本呢?(因为我想不通,为什么5个线程都能知道自己是在什么地方挂起,又需要从什么地方开始被唤醒,所以我觉得是不是每个线程都有一个TestA对象的副本,以记录是在什么地方被挂起的)
- Java code
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(); }}
------解决方案--------------------
up~~sofa!!!
------解决方案--------------------
5个线程的run方法本身并没有被挂起,如果run方法在testA.myTest();前面还有其它语句,而且是多CPU情况下它们就是并发/并行执行.
只有在执行到testA.myTest()时才产竞争,都在等待testA的锁.这个锁是唯一的.
------解决方案--------------------
------解决方案--------------------
你看下线程的优先级。。在未被设置(默认优先级)的时候如3楼所说。。完全由start()自己控制的