我有一个 Java 线程做这样的事情:
while (running) {
synchronized (lock) {
if (nextVal == null) {
try {
lock.wait();
} catch (InterruptedException ie) {
continue;
}
}
val = nextVal;
nextVal = null;
}
...do stuff with 'val'...
}
在其他地方我这样设置值:
if (val == null) {
LOG.error("null value");
} else {
synchronized (lock) {
nextVal = newVal;
lock.notify();
}
}
偶尔(字面意思是每几百万次一次)nextVal 将设置为空。我已经在记录消息中折腾了,我可以看到执行顺序如下所示:
- thread1 将 nextVal 设置为 newVal
- thread1 调用 lock.notify()
- thread2 从 lock.wait() 中唤醒
- thread2 将 val 设置为 nextVal
- thread2 将 nextVal 设置为 null
- thread2 用 val 做事
- thread2 调用 lock.wait()
- thread2 从 lock.wait() 中唤醒
- 没有其他线程调用 lock.notify() 并且 thread2 没有被中断
- thread2 将 val 设置为 nextVal(为空)
- 等等
我已经明确检查过,锁第二次被唤醒,它没有被中断。
我在这里做错了吗?