Question 132
Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized (Thread.currentThread()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which 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. Reversing the order of obj.wait() and obj.notify() may cause this
method to complete normally.
F. A call to notify() or notifyAll() from another thread may cause this
method to complete normally.答案是B。IllegalStateException怎么来的?

解决方案 »

  1.   

    notify(); 
    应该由别的线程发出,是线程间通信,是这样吧?那是obj.wait()等的时间太长了,导致错误还是obj.notify()发现没有线程在等他,导致的?
      

  2.   

    5天后我就考SCJP考试了!!!麻烦各位大侠帮忙解决疑惑!!!感激不尽!~~~
      

  3.   

    public final void wait() throws InterruptedException {
    wait(0);
        }
    wait方法可能抛出InterruptedException异常,这里没有捕捉,编译也不可能通过吧。