URL: http://blog.youkuaiyun.com/polarman/archive/2006/08/09/1042149.aspx
线程池通俗的描述就是预先创建若干空闲线程,等到需要用多线程去处理事务的时候去唤醒某些空闲线程执行处理任务,这样就省去了频繁创建线程的时间,因为频 繁创建线程是要耗费大量的CPU资源的。如果一个应用程序需要频繁地处理大量并发事务,不断的创建销毁线程往往会大大地降低系统的效率,这时候线程池就派 上用场了。
本文旨在使用Java语言编写一个通用的线程池。当需要使用线程池处理事务时,只需按照指定规范封装好事务处理对象,然后用已有的线程池对象去自动选择空 闲线程自动调用事务处理对象即可。并实现线程池的动态修改(修改当前线程数,最大线程数等)。下面是实现代码:
//ThreadTask .java
packagepolarman.threadpool;


/***//**
*线程任务
*@authorryang
*2006-8-8
*/

publicinterfaceThreadTask...{
publicvoidrun();
}
//PooledThread.java
packagepolarman.threadpool;

importjava.util.Collection;
importjava.util.Vector;


/***//**
*接受线程池管理的线程
*@authorryang
*2006-8-8
*/

publicclassPooledThreadextendsThread...{

protectedVectortasks=newVector();
protectedbooleanrunning=false;
protectedbooleanstopped=false;
protectedbooleanpaused=false;
protectedbooleankilled=false;
privateThreadPoolpool;


publicPooledThread(ThreadPoolpool)...{
this.pool=pool;
}


publicvoidputTask(ThreadTasktask)...{
tasks.add(task);
}


publicvoidputTasks(ThreadTask[]tasks)...{
for(inti=0;i<tasks.length;i++)
this.tasks.add(tasks[i]);
}


publicvoidputTasks(Collectiontasks)...{
this.tasks.addAll(tasks);
}


protectedThreadTaskpopTask()...{
if(tasks.size()>0)
return(ThreadTask)tasks.remove(0);
else
returnnull;
}


publicbooleanisRunning()...{
returnrunning;
}


publicvoidstopTasks()...{
stopped=true;
}


publicvoidstopTasksSync()...{
stopTasks();

while(isRunning())...{

try...{
sleep(5);

}catch(InterruptedExceptione)...{
}
}
}


publicvoidpauseTasks()...{
paused=true;
}


publicvoidpauseTasksSync()...{
pauseTasks();

while(isRunning())...{

try...{
sleep(5);

}catch(InterruptedExceptione)...{
}
}
}


publicvoidkill()...{
if(!running)
interrupt();
else
killed=true;
}


publicvoidkillSync()...{
kill();

while(isAlive())...{

try...{
sleep(5);

}catch(InterruptedExceptione)...{
}
}
}


publicsynchronizedvoidstartTasks()...{
running=true;
this.notify();
}


publicsynchronizedvoidrun()...{

try...{

while(true)...{

if(!running||tasks.size()==0)...{
pool.notifyForIdleThread();
//System.out.println(Thread.currentThread().getId()+":空闲");
this.wait();

}else...{
ThreadTasktask;

while((task=popTask())!=null)...{
task.run();

if(stopped)...{
stopped=false;

if(tasks.size()>0)...{
tasks.clear();
System.out.println(Thread.currentThread().getId()+":Tasksarestopped");
break;
}
}

if(paused)...{
paused=false;

if(tasks.size()>0)...{
System.out.println(Thread.currentThread().getId()+":Tasksarepaused");
break;
}
}
}
running=false;
}


if(killed)...{
killed=false;
break;
}
}

}catch(InterruptedExceptione)...{
return;
}

//System.out.println(Thread.currentThread().getId()+":Killed");
}
}
//ThreadPool.java
packagepolarman.threadpool;

importjava.util.Collection;
importjava.util.Iterator;
importjava.util.Vector;


/***//**
*线程池
*@authorryang
*2006-8-8
*/

publicclassThreadPool...{

protectedintmaxPoolSize;
protectedintinitPoolSize;
protectedVectorthreads=newVector();
protectedbooleaninitialized=false;
protectedbooleanhasIdleThread=false;


publicThreadPool(intmaxPoolSize,intinitPoolSize)...{
this.maxPoolSize=maxPoolSize;
this.initPoolSize=initPoolSize;
}


publicvoidinit()...{
initialized=true;

for(inti=0;i<initPoolSize;i++)...{
PooledThreadthread=newPooledThread(this);
thread.start();
threads.add(thread);
}

//System.out.println("线程池初始化结束,线程数="+threads.size()+"最大线程数="+maxPoolSize);
}


publicvoidsetMaxPoolSize(intmaxPoolSize)...{
//System.out.println("重设最大线程数,最大线程数="+maxPoolSize);
this.maxPoolSize=maxPoolSize;
if(maxPoolSize<getPoolSize())
setPoolSize(maxPoolSize);
}


/***//**
*重设当前线程数
*若需杀掉某线程,线程不会立刻杀掉,而会等到线程中的事务处理完成
*但此方法会立刻从线程池中移除该线程,不会等待事务处理结束
*@paramsize
*/

publicvoidsetPoolSize(intsize)
线程池通俗的描述就是预先创建若干空闲线程,等到需要用多线程去处理事务的时候去唤醒某些空闲线程执行处理任务,这样就省去了频繁创建线程的时间,因为频 繁创建线程是要耗费大量的CPU资源的。如果一个应用程序需要频繁地处理大量并发事务,不断的创建销毁线程往往会大大地降低系统的效率,这时候线程池就派 上用场了。
本文旨在使用Java语言编写一个通用的线程池。当需要使用线程池处理事务时,只需按照指定规范封装好事务处理对象,然后用已有的线程池对象去自动选择空 闲线程自动调用事务处理对象即可。并实现线程池的动态修改(修改当前线程数,最大线程数等)。下面是实现代码:
//ThreadTask .java
packagepolarman.threadpool;

/***//**
*线程任务
*@authorryang
*2006-8-8
*/
publicinterfaceThreadTask...{
publicvoidrun();
}
//PooledThread.java
packagepolarman.threadpool;
importjava.util.Collection;
importjava.util.Vector;

/***//**
*接受线程池管理的线程
*@authorryang
*2006-8-8
*/
publicclassPooledThreadextendsThread...{
protectedVectortasks=newVector();
protectedbooleanrunning=false;
protectedbooleanstopped=false;
protectedbooleanpaused=false;
protectedbooleankilled=false;
privateThreadPoolpool;

publicPooledThread(ThreadPoolpool)...{
this.pool=pool;
}

publicvoidputTask(ThreadTasktask)...{
tasks.add(task);
}

publicvoidputTasks(ThreadTask[]tasks)...{
for(inti=0;i<tasks.length;i++)
this.tasks.add(tasks[i]);
}

publicvoidputTasks(Collectiontasks)...{
this.tasks.addAll(tasks);
}

protectedThreadTaskpopTask()...{
if(tasks.size()>0)
return(ThreadTask)tasks.remove(0);
else
returnnull;
}

publicbooleanisRunning()...{
returnrunning;
}

publicvoidstopTasks()...{
stopped=true;
}

publicvoidstopTasksSync()...{
stopTasks();
while(isRunning())...{
try...{
sleep(5);
}catch(InterruptedExceptione)...{
}
}
}

publicvoidpauseTasks()...{
paused=true;
}

publicvoidpauseTasksSync()...{
pauseTasks();
while(isRunning())...{
try...{
sleep(5);
}catch(InterruptedExceptione)...{
}
}
}

publicvoidkill()...{
if(!running)
interrupt();
else
killed=true;
}

publicvoidkillSync()...{
kill();
while(isAlive())...{
try...{
sleep(5);
}catch(InterruptedExceptione)...{
}
}
}

publicsynchronizedvoidstartTasks()...{
running=true;
this.notify();
}

publicsynchronizedvoidrun()...{
try...{
while(true)...{
if(!running||tasks.size()==0)...{
pool.notifyForIdleThread();
//System.out.println(Thread.currentThread().getId()+":空闲");
this.wait();
}else...{
ThreadTasktask;
while((task=popTask())!=null)...{
task.run();
if(stopped)...{
stopped=false;
if(tasks.size()>0)...{
tasks.clear();
System.out.println(Thread.currentThread().getId()+":Tasksarestopped");
break;
}
}
if(paused)...{
paused=false;
if(tasks.size()>0)...{
System.out.println(Thread.currentThread().getId()+":Tasksarepaused");
break;
}
}
}
running=false;
}

if(killed)...{
killed=false;
break;
}
}
}catch(InterruptedExceptione)...{
return;
}
//System.out.println(Thread.currentThread().getId()+":Killed");
}
}
//ThreadPool.java
packagepolarman.threadpool;
importjava.util.Collection;
importjava.util.Iterator;
importjava.util.Vector;

/***//**
*线程池
*@authorryang
*2006-8-8
*/
publicclassThreadPool...{
protectedintmaxPoolSize;
protectedintinitPoolSize;
protectedVectorthreads=newVector();
protectedbooleaninitialized=false;
protectedbooleanhasIdleThread=false;

publicThreadPool(intmaxPoolSize,intinitPoolSize)...{
this.maxPoolSize=maxPoolSize;
this.initPoolSize=initPoolSize;
}

publicvoidinit()...{
initialized=true;
for(inti=0;i<initPoolSize;i++)...{
PooledThreadthread=newPooledThread(this);
thread.start();
threads.add(thread);
}
//System.out.println("线程池初始化结束,线程数="+threads.size()+"最大线程数="+maxPoolSize);
}

publicvoidsetMaxPoolSize(intmaxPoolSize)...{
//System.out.println("重设最大线程数,最大线程数="+maxPoolSize);
this.maxPoolSize=maxPoolSize;
if(maxPoolSize<getPoolSize())
setPoolSize(maxPoolSize);
}

/***//**
*重设当前线程数
*若需杀掉某线程,线程不会立刻杀掉,而会等到线程中的事务处理完成
*但此方法会立刻从线程池中移除该线程,不会等待事务处理结束
*@paramsize
*/
publicvoidsetPoolSize(intsize)
本文介绍了一种使用Java语言实现的通用线程池方案。通过预先创建多个空闲线程来处理并发任务,避免频繁创建线程带来的性能损耗。文章提供了线程池、线程任务及接受线程池管理的线程等核心组件的具体实现。
170万+

被折叠的 条评论
为什么被折叠?



