importjava.util.concurrent.CountDownLatch;public classCDDemo
{public static voidmain(String[] args)
{
CountDownLatch cd= new CountDownLatch(3);new Racer(cd, "A").start();new Racer(cd, "B").start();new Racer(cd, "C").start();for(int i=0; i<3; i++)
{try{
Thread.sleep(1000);
cd.countDown();
}catch(InterruptedException e)
{
e.printStackTrace();
}
System.out.println(i);if(i == 2)
System.out.println("start");
}
}
}class Racer extendsThread
{privateCountDownLatch countDownLatch;publicRacer(CountDownLatch countDownLatch, String name)
{
setName(name);this.countDownLatch =countDownLatch;
}
@Overridepublic voidrun()
{try{
countDownLatch.wait();
}catch(InterruptedException e1)
{
e1.printStackTrace();
}for(int i=0; i<3; i++)
{
System.out.println(getName()+" : "+i);
}
}
}
Exception in thread "B" Exception in thread "C" Exception in thread "A" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at com.lzyer.bingfa.Racer.run(CDDemo.java:49)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at com.lzyer.bingfa.Racer.run(CDDemo.java:49)
java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:503)
at com.lzyer.bingfa.Racer.run(CDDemo.java:49)
刚刚学习这部分内容,求解,谢谢。