日期:2014-05-20  浏览次数:20738 次

答案为什么是B?
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.


------解决方案--------------------
IllegalStateException:
在非法或不适当的时间调用方法时产生的信号。换句话说,即 Java 环境或 Java 应用程序没有处于请求操作所要求的适当状态下。

synchronized锁住的是当前线程这个对象,而当前线程正在使用中,所以属于非法或不适当的时间表调用.

不知道解释的对否,听高手的详解.:)
------解决方案--------------------
synchronized(Thread.currentThread()){
obj.wait();
obj.notify();

}
====================

obj.wait()是用于把当前线程抛入对象池等待,直到其它线程调用此对象的notify()方法将该线程唤醒....后面紧接着来了个obj.notify() 处于等待状态下的线程调用notify()方法唤醒自身?

属于非法调用
------解决方案--------------------
UP
------解决方案--------------------
UP
------解决方案--------------------
这里应该是都不对吧
应该抛出java.lang.IllegalMonitorStateException异常

因为你在调用obj.wait();的时候并没有对obj锁定,必须
synchronized(obj)之后才可以调用obj.wait()