"SCJP 1.6版考題 221" 修訂間的差異
(新頁面: Given: <br>7. void waitForSignal(){ <br>8. Object obj = new Object(); <br>9. synchronized(Thread.c...) |
(無差異)
|
於 2013年3月27日 (三) 16:44 的最新修訂
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()".
解答
Ans: B
解說: IllegalMonitorStateException是說一個執行緒試圖在未取得物件lock時,去對該物件進行等待(wait)或通知(notify)的動作,
會產生的一個不合法的監控狀態例外。
正確的程式寫法應如下:
synchronized(obj) {
}
取行obj的lock後,方能對obj進行wait或notify。
|