ITeye博客

工作中用到的线程池

wenqxin · 2010-06-23 09:33 · java基础 · 编程语言 · Markdown

工作中用到的线程池代码,单例模式写的,感觉不错.记下来.

public class ThreadPool{
	
    //公用线程
    public static final String commonThreads="commonThreads";
    //公用线程个数
    //TODO 可以配置
    public static final int commonThreadCount=2;
    //所有池列表
    private static List<ThreadPool> pools=new LinkedList<ThreadPool>();
	private static ThreadPool pool=null;
    /**
     * 线程池的唯一编号
     */
    private String poolId="default Thread pool thread";
    //----------------------
    /**
     * 任务队列
     */
    private TaskQueue taskQueue=new TaskQueue();
    /**
     * 线程池中所有线程列表
     */
    private List<PoolThread> threads=new LinkedList<PoolThread>();
    
    
    private ThreadPool(int threadCount,String id) {
        this.poolId=id;
        this.init(threadCount);
    }
    
    private ThreadPool(int threadCount) {
        this.init(threadCount);
    }
    
    private void init(int threadCount){
        for(int i=0;i<threadCount;i++){
               PoolThread poolThread=new PoolThread(this.taskQueue);
               threads.add(poolThread);
               poolThread.setDaemon(true);
               poolThread.setName(poolId);
               poolThread.start();
           }
    }
    /**
     * 添加一项任务到线程池
     * @param runnable
     */
    public void addTask(Runnable task){
        this.taskQueue.addTask(task);
    }
    /**
     * 获得指定ID的线程池对象,如果没创建,就创建,threadCount为对应线程池的线程个数
     * @param poolId
     * @param threadCount
     * @return
     */
    public static  ThreadPool getInstance(String poolId,int threadCount){
        ThreadPool pool=getPool(poolId);
        if(pool==null){
            pool=new ThreadPool(threadCount,poolId);
            pools.add(pool);
        }
        return pool;
    }
    /**
     * 返回指定ID的线程组,如果没有就返回空
     * @param poolId
     * @return
     */
    public static  ThreadPool getInstance(String poolId){
        ThreadPool pool=getPool(poolId);
        return pool;
    }
    /**
     * 获得进行公共事务处理的线程
     * @return
     */
    public static  ThreadPool getInstance(){
        return getInstance(commonThreads, commonThreadCount);
    }
    /**
     * 返回指定ID的线程池
     * @param id
     * @return
     */
    private static ThreadPool getPool(String id){
        ThreadPool ret=null;
        int len=pools.size();
        for(int i=0;i<len;i++){
            ThreadPool pool=pools.get(i);
            if(pool.getId().equals(id)){
                ret=pool;
                return ret;
            }
        }
        return ret;
    }
    public String getId(){
        return this.poolId;
    }
    

    
}

class TaskQueue{
    //任务链表
    private List<Runnable> tasks=new LinkedList<Runnable>();
    /**
     * 添加一任务到队列
     * @param task
     */
    public synchronized void addTask(Runnable task) {
      this.tasks.add(task);
      if(this.getTaskCount()<=1){//有线程被挂起
//    	  System.out.println("叫醒");
    	  this.notify();
      }
    }
    /**
     * 当前线程将被挂起,直到有可用的任务到达
     * @return
     */
    public synchronized Runnable getTask(){
        Runnable ret=null;
        while(true){
          if(this.getTaskCount()<=0){
              try {
//            	  System.out.println("wait......");
                this.wait();
            } catch (InterruptedException e) {}
          }else{
              ret=this.tasks.remove(0);
              break;
          }
        }
        return ret;
    }
    public synchronized int getTaskCount(){
        return this.tasks.size();
    }
}

/**
 * 线程池中的线程
 * 
 *
 */
class PoolThread extends Thread{
	
    private TaskQueue queue;
    PoolThread(TaskQueue queue){
        this.queue=queue;
    }
    
    public void run(){
        while(true){
            Runnable run=queue.getTask();
            run.run();
            //HelperMethods.sleepThread();//让出处理器
        }
    }
}


	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。