[LinkedIn] Implement a Semaphore

From Here
这是一个带有upperbound的semaphore。

public class BoundedSemaphore {
  private int signals = 0;
  private int bound   = 0;

  public BoundedSemaphore(int upperBound){
    this.bound = upperBound;
  }

  public synchronized void take() throws InterruptedException{
    while(this.signals == bound) wait();
    this.signals++;
    this.notify();
  }

  public synchronized void release() throws InterruptedException{
    while(this.signals == 0) wait();
    this.signals--;
    this.notify();
  }
}
posted on 2015-03-31 02:22  Seth_L  阅读(136)  评论(0编辑  收藏  举报