void waitForSignal(){
Object obj=new Object();
synchronized(Thread.currentThread()){
obj.wait();
obj.notify();

}
}what statement is ture?
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 replace with "((Thread)obj).wati()".
E.a call to notify() or notifyAll() from another thread may cause this method to complete normally.

解决方案 »

  1.   

    wait
    public final void wait()
                    throws InterruptedException在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。换句话说,此方法的行为就好像它仅执行 wait(0) 调用一样。 
    当前线程必须拥有此对象监视器。该线程发布对此监视器的所有权并等待,直到其他线程通过调用 notify 方法,或 notifyAll 方法通知在此对象的监视器上等待的线程醒来。然后该线程将等到重新获得对监视器的所有权后才能继续执行。 对于某一个参数的版本,实现中断和虚假唤醒是可能的,而且此方法应始终在循环中使用: synchronized (obj) {
    while (<condition does not hold>)
    obj.wait();
    ... // Perform action appropriate to condition
         }
     此方法只应由作为此对象监视器的所有者的线程来调用。有关线程能够成为监视器所有者的方法的描述,请参阅 notify 方法。 抛出: 
    IllegalMonitorStateException - 如果当前线程不是此对象监视器的所有者。 
    InterruptedException - 如果在当前线程等待通知之前或者正在等待通知时,任何线程中断了当前线程。在抛出此异常时,当前线程的中断状态 被清除。
    注意看它抛出的异常,很明显obj  没有锁定当前监视器,也就是说obj没有抢到这把锁(synchronized),你锁定的是Thread.currentThread(),答案是b
    付:
    将Thread.currentThread(),改成obj就不会有异常了!!
      

  2.   

    将Thread.currentThread(),改成obj就不会有异常了!!
      

  3.   

    原理在这里:http://www.ticmy.com/?p=219