工作中用到的线程池

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

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();//让出处理器
}
}
}


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值