publicclassMain { publicstaticvoidmain(String[] args)throws InterruptedException { First first = new First(); Second second = new Second(first); synchronized( second ) { System.out.println("I'm faster than second~"); second.notifyAll(); } } }
publicclassMain { publicstaticvoidmain(String[] args)throws InterruptedException { First first = new First(); Second second = new Second(first); System.out.println("wating for all threads prepared~"); Thread.sleep(2000); synchronized( second ) { System.out.println("I'm faster than second~"); second.notifyAll(); } } }
输出结果:
1 2 3 4
wating for all threads prepared~ I’m faster than second~ I’m faster than first~ hello world~