java - notify 会立即调用等待线程吗?
问题描述
我正在编写一个 Java 代码,其中在满足特定条件后在等待线程上调用 notify,但等待线程不会立即开始运行。可能是什么原因?
解决方案
wait
(以及notify
)需要进行同步监视器。如果调用的代码notify
没有释放它,那么wait
将继续“等待”直到监视器被释放
// wait thread
synchronized (syncObject) {
syncObject.wait(); // to proceed to next line,
// this thread must wait until notify is called
// and then take ownership over syncObject
// next line
}
...
// notify thread
synchronized (syncObject) {
syncObject.wait();
while (true) {}; // infinite loop, syncObject is never released,
// wait thread will never gain ownership over syncObject
// and will never wake up
}
推荐阅读
- jquery - javascript - 异步问题 - 数据未准备好
- javascript - 如何从 ng-repeat 中的元素调用 javascript 函数
- javascript - 如何使用数组元素缩放 Javascript 中的矩形?
- django - 如何在查询中单独获取字符串而不是元组?
- haproxy - 如何在Haproxy中根据url参数形成一个stick table
- angular - 根据Angular中父组件到子组件的选项传递动态Html元素?
- r - 在ggplot中为具有相同数量的箱绘制平均x和平均y?
- flutter - 如何在 DropdownButton 中更改 Flutter DropdownMenuItem 的宽度/填充?
- c# - 使用 Selenium Webdriver C# 和 Chrome 浏览器弹出窗口中的“允许多次下载”
- python - 从熊猫列中删除重复的字母只存在于彼此,Python