foo
オブザーバーを受け取り、オブザーバーがウェイクアップするまで現在のスレッドをスリープ状態にする次のメソッドがあります。
何らかの理由で、java.lang.IllegalMonitorStateException
例外が発生し続けますfoo
public void foo(Observer o)
{
Thread currentThread = Thread.currentThread();
o.setThread(currentThread);
// sleep until the observer wakes it
currentThread.wait(2000); // <<<<< Exception happens here
}
Observer
オブジェクトは、currentThread.notifyAll()
後で呼び出したときにObservable
呼び出しますupdate
。
public class Observer
{
private volatile Thread currentThread;
// ... other code ....
public void setThread(Thread t)
{
currentThread = t;
}
public void update(Observable o)
{
currentThread.notify();
}
}
ここで何が間違っているのですか?