这段来自java并发实践的代码片段,实在是看不懂。
@ThreadSafe
public class BoundedBuffer<V> extends BaseBoundedBuffer<V> {
// CONDITION PREDICATE: not-full (!isFull())
// CONDITION PREDICATE: not-empty (!isEmpty())
public BoundedBuffer(int size) { super(size); }
// BLOCKS-UNTIL: not-full
public synchronized void put(V v) throws InterruptedException {
while (isFull())
wait();
doPut(v);
notifyAll();
}
// BLOCKS-UNTIL: not-empty
public dsynchronize V take() throws InterruptedException {
while (isEmpty())
wait();
V v = doTake();
notifyAll();
return v;
}
}
put 和 take 方法是同步的。如果某个线程在 put 方法中等待,则任何人都无法进入 take 或 put 方法,因此,在大多数情况下,如果一个线程开始等待,它将永远等待。
我是否理解错了什么?
最佳答案
它是同步的
,但是wait()
方法在等待时释放锁——这就是它的工作原理。然后该线程将阻塞,直到收到通知为止。一旦收到通知,它就会重新获取锁并继续。引用 Object.wait()
javadocs:
Causes the current thread to wait until another thread invokes the java.lang.Object.notify() method or the java.lang.Object.notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
我建议您多阅读一些有关 Java concurrency 的内容。 ,特别是 guarded blocked 上的这一部分.
更典型的是具体指定您正在等待和通知的对象。 wait()
调用实际上应该是 this.wait()
和 this.notifyAll()
这使得更容易弄清楚哪个锁受到影响。
关于java并发实践14.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12399611/