一个线程池的例子注释用的是E文,见笑ing/* * @(#)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 }
* @(#)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
}