线程池是在服务器一类的机器上反复创造和销毁线程很浪废CPU资源,于是就一次创建N个线程,反复使用,使用完之后放入一个由可利用线程组成的链表,要用的时候再从链表中取
解决方案 »
- 求IPMI java实例
- java version "1.5.0_13" 不能使用泛型?
- 这个程序为何会出现两处错误提示:The constructor Date(int,int,int) is deprecated 和 The method getYear() from the type Date is de
- 如何实现多维Hash表并能快速存取
- 看到此帖的高人请帮我一个小忙
- 关于java调用exe的问题——急待解决方案,在线等待高人
- 请问参加SCJP考试要注意一些什么?
- 一个处理查询的问题
- Nokia MobileSet中如何让手机识别中文?
- 再送40分,关于 out.print("中文") 乱码问题
- 我的classpath路径前加有‘.'怎么还是不能在当前目录下编译?
- 怎样解决用jbuilder生成的.exe文件运行时需要Java Runtime Environment?
注释用的是英语,不要见笑 -_-~
/*
* @(#)ThreadPool.java 1.0 04/03/06
*
* Copyright 2004 zs02314.
*/
package hdx.instance;import java.util.*;/**
* A Thread Pool which possesses a specify size of Threads.
* It may improve the performance of your application if the application create threads constantly.
*
* Usage example:
* ThreadPool tp=new ThreadPool(20);
* tp.runTask(thread);//thread is a Thread created in your code
*
* @beaninfo
* description: A Thread Pool which possesses a specify size of Threads.
*
* @version 1.0 04/03/06
* @author Hodex
*/
public class ThreadPool{
/* The list of free-Threads,in-use-Threads */
private Vector freeThreads=new Vector();
private Vector inUseThreads=new Vector();
/* the capacity of the ThreadPool */
private int size;
/**
* Creates a ThreadPool with a default size.
*
*/
public ThreadPool(){
this(10);
}
/**
* Creates a ThreadPool with a size
*
* @param size the capacity of the ThreadPool
* @see #createPool
* @see #getSize
*/
public ThreadPool(int size){
this.size=size;
createPool(size);
}
/**
* Factory method which create a Thread Pool.
*
* @param s the capacity of the ThreadPool
* @see PThread
*/
protected void createPool(int s){
for(int i=0;i<s;i++){
PThread pt=new PThread(this);
pt.start();
freeThreads.add(pt);
}
try{
Thread.sleep(2000);//ensure every Pthread has been created
}catch(InterruptedException e){
e.printStackTrace();
}
}
/**
* Run the task specified
*
* @param task a Runnable object which need to run in a thread
* @see PThread#setTask
*/
public synchronized void runTask(Runnable task){
if(freeThreads.isEmpty()){
throw new RuntimeException("Busy now! please wait and try again.");
}
PThread t= (PThread)freeThreads.remove(0);
inUseThreads.add(t);
t.setTask(task);
}
/**
* Free a thread. Usually,it will be invoked automatically when a task finished.
*
* @param t a PThread with a finished task.
* @see PThread#executeTask
*/
protected synchronized void free(PThread t){
inUseThreads.remove(t);
freeThreads.add(t);
}
/**
* Returns the capacity of the ThreadPool
*/
public int getSize(){
return this.size;
}
/**
* This class is used to provide a reusable thread
*/
class PThread extends Thread{
boolean flag=true;
Runnable task=null;
ThreadPool pool;
PThread(ThreadPool pool){
this.pool=pool;
}
synchronized void setTask(Runnable task){
this.task=task;
notifyAll();
}
private synchronized void executeTask(){
while(flag){//you can add some methods to control the flag
try{
if(task==null){
wait();
}
}catch(InterruptedException e){
e.printStackTrace();
}
task.run();
task=null;
pool.free(this);
}
}
public void run(){
executeTask();
}
}//end of class PThread
}解释一下原理:首先创建若干个线程//size每个线程处于等待状态//wait();当调用runTask时->setTask->唤醒指定线程//notify在此线程中(注意)运行待工作任务//task.run();运行完后,该线程继续进入等待状态,并没销毁 //task=null