SCJP題庫第234題

 
Given:
foo and bar are public references available to many other threads, foo refers to a Thread and bar is an Object. The thread foo is currently executing bar.wait().
From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?
A. foo.notify();
B. bar.notify();
C. foo.notifyAll();
D. Thread.notify();
E. bar.notifyAll();
F. Object.notify();

Ans: E

解說:foo和bar是可讓其他執行緒存取的公開參考,foo參考至一個執行緒(Thread),bar是一個Object物件,執行緒foo正在執行bar.wait()


從另一個執行緒,可以提供一個最可靠的方式來確認foo可以停止執行wait方法?


由於是對bar停止wait,所以答案不是B就是E,notify與notifyAll的差異在於一個wait池中可能會有多個執行在等待bar,若單下notify,只有一個wait池中的執行緒可以離開wait池,這個執行緒未必是foo這個執行緒,而notifyAll的意思是讓所有等待bar的執行緒全數離開bar的wait池。

Comments

Popular posts from this blog

Android-使用webview在V3版的Google地圖GPS定位