Given:
7. void waitForSignal() {
8. Object obj = new Object();
9. synchronized (Thread.currentThread()) {
10. obj.wait();
11. obj.notify();
12. }
13. }
Which statement is true?
A. This code can throw an InterruptedException.
B. This code can throw an IllegalMonitorStateException.
C. This code can throw a TimeoutException after ten minutes.
D. Reversing the order of obj.wait() and obj.notify() might cause this method to complete normally.
E. A call to notify() or notifyAll() from another thread might cause this method to complete
normally.
F. This code does NOT compile unless "obj.wait()" is replaced with "((Thread)
obj).wait()".
答案是?(还请写出为什么)

解决方案 »

  1.   

    This code can throw an InterruptedException.
     如果在当前线程等待通知之前或者正在等待通知时,任何线程中断了当前线程。在抛出此异常时,当前线程的中断状态 被清除。
      

  2.   

    如果当前的线程不是此对象锁的所有者,却调用该对象的notify(),notify(),wait()方法时抛出IllegalMonitorStateException异常。
    jdk1.6apidoc:
    Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.
      

  3.   

    注意第9、10、 11行第9行是以ThreadCurrent为锁对象而第10和11行执行wait和notify的是obj对象故会抛出IllegalMonitorStateException楼上正解