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
}
}
}
}
}
22
分享到:
相关推荐
A simple C++11 Thread Pool implementation. Basic usage: // create thread pool with 4 worker threads ThreadPool pool(4); // enqueue and store future auto result = pool.enqueue([](int answer) { ...
很好用的boost线程池,实际例子在test目录
线程池的使用
threadpool is a cross-platform C++ thread pool library
CodeProject Thread Basics and the CLR's Thread Pool_ Free source code and programming help
A simple C Thread pool implementation
Socket Programming & Thread Pool实例
Thread pool implementation using c++11 threads
Modern and efficient C++ Thread Pool Library
A simple C++11 Thread Pool implementation
A minimal but powerful thread pool in ANSI C
Linux 线程池(Thread Pool)的实现原理 实例
Smart Thread Pool is a thread pool written in C#. The implementation was first based on Stephan Toub's thread pool with some extra features, but now it is far beyond the original. Here is a list of ...
主要介绍了阿里dubbo出错提示Thread pool is EXHAUSTED的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Qt thread pool 线程池样例,信号槽,跨线程 lambda,使用样例;
thread thread thread thread thread thread thread
.net中thread pool使用举例,代码为C#编写。
Intel发布的线程池示例源代码,作为服务器处理良好的示例。
多线程池试验,很简单得,适合初学者,讲解了多线程的实验,下载就可以直接运行