线程池

简介: 引用:http://www.west263.com/www/info/22460-1.htm 任何游戏都至少需要运行两个线程,主线程和gui线程 而线程池是一个管理运行线程的有用工具,下面的代码示范了一个线程池的实现方法~~ ************************************************ (threadpool.

引用: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"); 

}; 



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

相关文章
|
1月前
|
缓存 Java
|
3月前
|
缓存 Java API
厉害了,线程池就该这么玩
厉害了,线程池就该这么玩
25 0
|
8月前
|
缓存 Java
线程池简单总结
线程池简单总结
61 0
|
9月前
|
缓存 Java 调度
线程池的介绍
线程池的介绍
|
9月前
|
前端开发 Java 调度
|
9月前
|
前端开发 Java 调度
你了解线程池吗
你了解线程池吗
52 0
|
9月前
|
Java 数据库连接 容器
关于线程池
关于线程池
56 0
|
9月前
|
存储 缓存 Java
理解与实现线程池
理解与实现线程池
108 0
|
10月前
|
存储 Java 测试技术
13.一文彻底了解线程池
大家好,我是王有志。线程池是Java面试中必问的八股文,涉及到非常多的问题,今天我们就通过一篇文章,来彻底搞懂Java面试中关于线程池的问题。
369 2
13.一文彻底了解线程池
|
10月前
|
设计模式 Java C++