线程池

引用:http://www.west263.com/www/info/22460-1.htm

任何游戏都至少需要运行两个线程,主线程和gui线程 
而线程池是一个管理运行线程的有用工具,下面的代码示范了一个线程池的实现方法~~ 
************************************************ 
(threadpool.java) 
import java.util.linkedlist; 

/** 
线程池是一组线程,限制执行任务的线程数 
*/ 
public class threadpool extends threadgroup { 

private boolean isalive; 
private linkedlist taskqueue; 
private int threadid; 
private static int threadpoolid; 

/** 
创建新的线程池,numthreads是池中的线程数 
*/ 
public threadpool(int numthreads) { 
super("threadpool-" + (threadpoolid++)); 
setdaemon(true); 

isalive = true; 

taskqueue = new linkedlist(); 
for (int i=0; i<numthreads; i++) { 
new pooledthread().start(); 


/** 
请求新任务。人物在池中下一空闲线程中运行,任务按收到的顺序执行 
*/ 
public synchronized void runtask(runnable task) { 
if (!isalive) { 
throw new illegalstateexception();//线程被关则抛出illegalstateexception异常 

if (task != null) { 
taskqueue.add(task); 
notify(); 





protected synchronized runnable gettask() 
throws interruptedexception 

while (taskqueue.size() == 0) { 
if (!isalive) { 
return null; 

wait(); 

return (runnable)taskqueue.removefirst(); 



/** 
关闭线程池,所有线程停止,不再执行任务 
*/ 
public synchronized void close() { 
if (isalive) { 
isalive = false; 
taskqueue.clear(); 
interrupt(); 




/** 
关闭线程池并等待所有线程完成,执行等待的任务 
*/ 
public void join() { 
//告诉等待线程线程池已关 
synchronized (this) { 
isalive = false; 
notifyall(); 


// 等待所有线程完成 
thread[] threads = new thread[activecount()]; 
int count = enumerate(threads); 
for (int i=0; i<count; i++) { 
try { 
threads[i].join(); 

catch (interruptedexception ex) { } 




/** 
用于进行任务的线程 
*/ 
private class pooledthread extends thread { 


public pooledthread() { 
super(threadpool.this, 
"pooledthread-" + (threadid++)); 



public void run() { 
while (!isinterrupted()) { 

// 得到任务 
runnable task = null; 
try { 
task = gettask(); 

catch (interruptedexception ex) { } 

// 若gettask()返回null或中断,则关闭此线程并返回 
if (task == null) { 
return; 


// 运行任务,吸收异常 
try { 
task.run(); 

catch (throwable t) { 
uncaughtexception(this, t); 





********************************************* 
要测试这个线程池,可以通过下面这个test程序! 
********************************************* 
(threadpooltest.java) 
public class threadpooltest { 

public static void main(string[] args) { 
if (args.length != 2) { 
system.out.println("tests the threadpool task."); 
system.out.println( 
"usage: java threadpooltest numtasks numthreads"); 
system.out.println( 
" numtasks - integer: number of task to run."); 
system.out.println( 
" numthreads - integer: number of threads " + 
"in the thread pool."); 
return; 

int numtasks = integer.parseint(args[0]); 
int numthreads = integer.parseint(args[1]); 

// 生成线程池 
threadpool threadpool = new threadpool(numthreads); 

// 运行任务 
for (int i=0; i<numtasks; i++) { 
threadpool.runtask(createtask(i)); 


// 关闭线程池并等待所有任务完成 
threadpool.join(); 



/** 
一个简单的任务(打印id) 
*/ 
private static runnable createtask(final int taskid) { 
return new runnable() { 
public void run() { 
system.out.println("task " + taskid + ": start"); 

// 增加耗时 
try { 
thread.sleep(500); 

catch (interruptedexception ex) { } 

system.out.println("task " + taskid + ": end"); 

}; 



****************************************************** 
这样的线程池可以在许多地方应用! 

你可能感兴趣的:(线程池)