java线程池

package com.dhcc.itsm.core.util;

/**
 * 线程池对象
 * @version 1.0
 * @author liang
 */
import java.util.LinkedList;
import java.util.List;

import javax.transaction.Synchronization;
@SuppressWarnings("unused")
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;
    }
    
    
    
    
    /**
     * 测试
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
		
    	ThreadPool threadPool = ThreadPool.getInstance("aaa",1);
    	threadPool.addTask( new printMsg("第1个线程") );
    	threadPool.addTask( new printMsg("第2个线程") );
    	
    	threadPool = ThreadPool.getInstance("bbb",1);
    	threadPool.addTask( new printMsg("第3个线程") );
    	threadPool.addTask(new printMsg("第4个线程"));
    	
    	threadPool = ThreadPool.getInstance("ccc",1);
    	threadPool.addTask( new printMsg("第5个线程") );
    	threadPool.addTask(new printMsg("第6个线程"));
    	
    	threadPool = ThreadPool.getInstance("ddd",1);
    	threadPool.addTask( new printMsg("第7个线程") );
    	threadPool.addTask(new printMsg("第8个线程"));
	    	
    	System.out.println("2.此部分无需等待响应!");
	}
    
}
/**
 * 任务队列类,提供任务集合的互斥访问
 * @author jk
 *
 */
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();
    }
}

/**
 * 线程池中的线程
 * @author jk
 *
 */
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();//让出处理器
        }
    }
}

/**
 * 执行的具体业务测试类
 * @author sony
 *
 */
class printMsg implements Runnable{

	private String msg ;
	private  int cont;
	
	public printMsg(String msg){
		this.msg = msg;
	}
	
	public void run() {

			for(int i=0;i<10;i++){
			      System.out.println("线程启动数: "+cont+" : "+msg);
			}
	}
}

你可能感兴趣的:(java,多线程,thread,windows)