根据Java文档,当obj1.wait()发生时,“线程释放该监视器的所有权,并等待直到另一个线程通知…”
因此,当当前线程正在等待时,obj1的锁被释放.但是其他所有锁呢?代码段可能会锁定两个对象:
synchronized(obj2){
f1();
synchronized(obj1){
f2();
obj1.wait();
}
}
Obj2将不会被释放,但是该线程不会运行,另一个线程将等待徒劳地释放obj2 …我不明白继续锁定obj2的原因.但是,好的,就是这样.
但是如何更好地组织这种等待,如何在等待所有或至少几个当前锁的时间内解锁?
解决方法:
对于您的示例,您可以将obj2替换为ReentrantLock:
Lock lock2 = new ReentrantLock();
try {
// Blocks until the lock is acquired, just like a `synchronized` statement
lock2.lock();
f1();
synchronized (obj1) {
f2();
lock2.unlock();
obj1.wait();
lock2.lock();
}
}
// Use `finally` to make sure the lock is always released, even if an exception is thrown
finally {
// Exception might have been thrown before current thread could acquire lock again, cannot
// unlock then
if (lock2.isHeldByCurrentThread()) {
lock2.unlock();
}
}
但是,这将允许另一个线程在当前线程开始等待obj1之前获取lock2.如果不希望这样,可以将obj1替换为Lock并等待obj2.