poolablethread.java

来自「java网络编程」· Java 代码 · 共 49 行

JAVA
49
字号
package multithread;



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

public class PoolableThread extends Thread{

  Runnable task=null;
  ThreadPool pool;

  public PoolableThread(ThreadPool pool) {
    this.pool=pool;
  }

  synchronized void setTask(Runnable task){
    this.task=task;
    notify();

  }

  synchronized void excuteTasks(){
    for(;;){
      try{
        if (task==null){
          wait();
        }
      }catch (InterruptedException ie){

      }
      task.run();
      task=null;
      pool.free(this);

    }
  }

  public void run(){
    excuteTasks();
  }
}

⌨️ 快捷键说明

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