0
我阅读本教程在http://tutorials.jenkov.com/java-concurrency/semaphores.html为什么在这段代码中,信号量会增加而不是减少?
而且它计数信号的实现是:
public class CountingSemaphore {
private int signals = 0;
public synchronized void take() {
this.signals++;
this.notify();
}
public synchronized void release() throws InterruptedException{
while(this.signals == 0) wait();
this.signals--;
}
}
代码觉得奇怪,我因为从我个人理解,信号记录多少可用资源,有,所以当我们take()
应该减少计数,并且当我们release()
(当然以限制开始)增加计数。
任何人都可以提供我更多的洞察在计数和倒计时方法之间有什么区别?
整个'take' /'release'是倒退的 - 'release'应该通知,而不是等待。是的,计数也是倒退。我建议你联系作者。 – 2014-10-02 16:56:02