日期:2014-05-20 浏览次数:20690 次
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(); } }