rt
希望大家把自己用的都贴出来对比一下
希望大家把自己用的都贴出来对比一下
解决方案 »
- 用JFileChooser实现的文件选择器单击文件时出现重命名错误
- 23分59秒59秒加1秒问题,请高手支招
- 出现这种情况怎么办?
- 用ibatis的动态sql出问题了
- swt中List有没有办法设置两列数据?
- 请熟悉java深层语法的朋友进来帮小弟一个忙,在线等待中..........................!
- //晕倒,main方法也能被子类继承................?
- 求《thinking in java》书的所有源代码!及时给分!
- 哪里能够下载java II 核心技术(共两卷)的电子文档???
- netty服务端如何向客户端发送消息
- 这是为什么?
- mysql如何同时访问两个数据库
public class WorkQueue
{
private final int nThreads;
private final PoolWorker[] threads;
private final LinkedList queue; public WorkQueue(int nThreads)
{
this.nThreads = nThreads;
queue = new LinkedList();
threads = new PoolWorker[nThreads]; for (int i=0; i<nThreads; i++) {
threads[i] = new PoolWorker();
threads[i].start();
}
} public void execute(Runnable r) {
synchronized(queue) {
queue.addLast(r);
queue.notify();
}
} private class PoolWorker extends Thread {
public void run() {
Runnable r; while (true) {
synchronized(queue) {
while (queue.isEmpty()) {
try
{
queue.wait();
}
catch (InterruptedException ignored)
{
}
} r = (Runnable) queue.removeFirst();
} // If we don't catch RuntimeException,
// the pool could leak threads
try {
r.run();
}
catch (RuntimeException e) {
// You might want to log something here
}
}
}
}
}
jdk1.5里的java.util.concurrent.ThreadPoolExecutor 非常好用。