且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在没有 IllegalMonitorStateException 的情况下在 Java 中使用等待和通知?

更新时间:2023-01-11 07:43:22

为了能够调用notify() 你需要在同一个对象上同步.

To be able to call notify() you need to synchronize on the same object.

synchronized (someObject) {
    someObject.wait();
}

/* different thread / object */
synchronized (someObject) {
    someObject.notify();
}