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

这里的IllegalStateException怎么来的?
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怎么来的?


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