假设我有以下情况:
而
synchronized void someMethod() {
...
try {
wait();
}catch(InterruptedException e) {
System.out.println("InterruptedException caught");
}
...
}
并且
synchronized void someOtherMethod() {
...
notify();
}
而
线程(Thread)
首先访问someMethod
,进入wait
状态,然后someOtherMethod
通知它并返回Runnable
状态。在方法中调用notify()
的位置是否重要?即使我将notify()
调用放置在方法内部的不同位置,行为也没有变化。
当调用notify()
时,线程(Thread)
应立即得到通知,难道不是吗?
Runnable
吗?唤醒的线程并没有被阻塞,它只是处于可运行状态,等待被执行。 - Cratylusif(condition) wait()
,而另一个线程执行makeConditionTrue(); notify(); doSomethingElse();
。 - selig