semaphore.java

来自「java实现的一个pascal编译器」· Java 代码 · 共 44 行

JAVA
44
字号
/**
 * File : Semaphore.java
 * Author : Fidel Viegas (viegasfh@hotmail.com)
 * Date : 23/02/2000
 */
package uk.co.brainycreatures.jpascal.type;

public class Semaphore {
  private long value; // value of semaphore

  // constructor for binary semaphores
  public Semaphore() {
    value = 0;
  }

  // constructor for counting semaphores
  public Semaphore(long initial) {
    value = initial; // initial value of the semaphore
  }
    
  // if the value of the semaphore is
  // greater than zero, then its value is decremented
  // by one
  public synchronized void Wait() {
    while (value <= 0) {
      try {
        wait();
      }
      catch (InterruptedException e) {
      }
    }
    value--;
  }

  // if the processes are blocked on the semaphore,
  // then unblock one of them
  public synchronized void Signal() {
    value++;
    notify(); // notify the process
  }
}

      

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?