csemaphore.java

来自「JAVA实现的短信网关程序」· Java 代码 · 共 51 行

JAVA
51
字号
package sms.PHS;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class CSemaphore {
  private String description;
  private boolean inuse;
  public CSemaphore(String s) {
    description = s;
    inuse = false;
  }

  public synchronized void finished() {
    //System.out.println("===========this is finished========================");
    inuse = false;
    notify();
  }

  public synchronized void use() throws InterruptedException {

    if (inuse) {
      try {
        System.out.println("===========this is use========================");
        wait();
        //inuse = true;
      }
      catch (InterruptedException e) {
        throw new InterruptedException(String.valueOf(String.valueOf(
            description)).concat(" interrupted"));
      }
    }
    //else {
      inuse = true;
    //}

    //InterruptedException _ex;
    //description = "";

    //throw new InterruptedException(String.valueOf(String.valueOf(description)).
    //                               concat(" interrupted"));
  }

}

⌨️ 快捷键说明

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