⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadobject.java

📁 pso源程序
💻 JAVA
字号:
/**
 * Description: provide the operations for a thread.
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    May 31, 2000    xiaofengxie@tsinghua.org.cn
 *
 * @version 1.0
 */

package Global.system;

import Global.basic.*;
import Global.define.*;

public abstract class ThreadObject extends BasicAttrib implements Runnable {
  protected static final int FLAG_STOP = 2;    //indicate a normal thread
  protected static final int FLAG_PAUSE = 1;    //indicate a normal thread
  protected static final int FLAG_RUNNING = 0;    //indicate a normal thread

  protected static final int NORMALTYPE = 0;    //indicate a normal thread
  protected static final int DLLTYPE = 1;       //indicate a thread that has dynamic lib
  protected int threadObjectType = NORMALTYPE;    //the type of the threadObject

  protected boolean debug = false;//Flag used to debug: true - output debug infomation; false - no debug information
  protected int run_flag = FLAG_STOP;
  protected volatile Thread blinker = null;  // The thread of AbstractSimulator

  protected String workDir = "";
  protected String tempDir = "";

  public ThreadObject() {
    super();
    workDir = GlobalPath.CURRENT_WORK_PATH;
    tempDir = GlobalPath.CURRENT_TEMP_PATH;
    name = getClass().getName();
  }

/**
 * Causes this thread to begin execution; the Java Virtual Machine
 * calls the <code>run</code> method of this thread.
 * <p>
 * The result is that two threads are running concurrently: the
 * current thread (which returns from the call to the
 * <code>start</code> method) and the other thread (which executes its
 * <code>run</code> method).
 *
 * @exception  IllegalThreadStateException  if the thread was already
 *               started.
 * @see        java.lang.Thread#run()
 * @see        java.lang.Thread#stop()
 */
  public void start() {
      blinker = new Thread(this);
      blinker.start();
      run_flag = FLAG_RUNNING;
  }

/**
 * Forces the thread to stop executing.
 */
  public void stop() {
    if (isAlive()) {
      run_flag = FLAG_STOP;
      switch(threadObjectType) {
        case NORMALTYPE:
          if(blinker!=null){
            blinker.stop();
            blinker = null;
          }
          System.out.println(getKey()+" is stopped.");
          break;
        case DLLTYPE:
          System.out.println("Please wait for a while for the "+getObjectName()+" to stop...");
          break;
        default:
          break;
      }
    }
  }

/**
 * Pause the thread.
 */
  public void pause() {
    if (isAlive()) {
      if (run_flag == FLAG_STOP) {
        return;
      }
      run_flag = FLAG_PAUSE;
      switch(threadObjectType) {
        case NORMALTYPE:
          blinker.suspend();
          System.out.println(getKey()+" is paused.");
          break;
        case DLLTYPE:
          System.out.println("Please wait for a while for the "+getObjectName()+" to pause...");
          break;
        default:
          break;
      }
    }
  }

/**
 * resume the thread.
 */
  public void resume() {
    if (isAlive()) {
      if (run_flag == FLAG_STOP) {
        return;
      }
      run_flag = FLAG_RUNNING;
      switch(threadObjectType) {
        case NORMALTYPE:
          blinker.resume();
          break;
        case DLLTYPE:
          break;
        default:
          break;
      }
      System.out.println(getKey()+" resumes...");
    }
  }

/**
 * Detect if the thread is alive.
 */
  public boolean isAlive() {
    return FLAG_STOP!=this.run_flag;
  }

  /**
   * Check if the current RunObject is running.
   */
    public boolean isRunning() {
      if (isAlive()) {
        System.out.println(getObjectName()+": is still running, please stop it first.");
        return true;
      }
      return false;
    }

/**
 * Set the running flag, called by the main system controller.
 */
  protected void setRunFlag(int run_flag){
    this.run_flag = run_flag;
  }

  public void setObjectName(String name) {
    this.name = name;
  }

  public String getObjectName(){
    return (this.name);
  }

  public int getRunFlag() {
    return(run_flag);
  }

  public String getKey() {
    return this.getClass().getName();
  }

}

⌨️ 快捷键说明

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