void waiForSignal() throws Exception{Object obj=new Object();
synchronized(Thread.currentThread()){
obj.wait();
obj.notify(); }
}
What statement is true?
A. This code may throw an InterruptedException.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
E. A call to notify() or notifyAll() from another thread may cause this method to complete normally.
Answer: (B)
10行应调用 Thread.currentThread().wait(); 或者 修改第九行synchronize(obj){
参考大纲:多线程这道题中的“10行应调用 Thread.currentThread().wait();”不明白,有请高手讲解.
synchronized(Thread.currentThread()){
obj.wait();
obj.notify(); }
}
What statement is true?
A. This code may throw an InterruptedException.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
E. A call to notify() or notifyAll() from another thread may cause this method to complete normally.
Answer: (B)
10行应调用 Thread.currentThread().wait(); 或者 修改第九行synchronize(obj){
参考大纲:多线程这道题中的“10行应调用 Thread.currentThread().wait();”不明白,有请高手讲解.
只有获得了锁的对象才能调用wait和notify方法
如果还不明白,说明楼主对于wait和notify的作用还不知道,可以查查