controlledthread.java

来自「JAVA面向对象编程_孙卫琴yaunma」· Java 代码 · 共 40 行

JAVA
40
字号
package control;
public class ControlledThread extends Thread {
  public static final int SUSP=1;
  public static final int STOP=2;
  public static final int RUN=0;

  private int state = RUN;

  public synchronized void setState( int state){
    this.state = state;
    if (state == RUN)
     notify();
  }

  public synchronized boolean checkState() {
    while(state == SUSP){
      try{
        System.out.println(Thread.currentThread().getName()+":wait");
        wait();
      }catch (InterruptedException e){throw new RuntimeException(e.getMessage()); }
    }
    if (state == STOP){
      return false;
    }
    return true;
  }
}



/****************************************************
 * 作者:孙卫琴                                     *
 * 来源:<<Java面向对象编程>>                       *
 * 技术支持网址:www.javathinker.org                *
 ***************************************************/

 


⌨️ 快捷键说明

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