我正在创建自己的线程池和可以并行执行可调用接口(interface)的 future 对象。 Executor 提供了 shutdown 方法来停止所有工作线程的运行。如果我正在创建如下所示的线程池,我应该如何实现关闭方法以在所有线程执行完毕后停止?
我的自定义线程池看起来像 this
class MyThreadPool implements java.util.concurrent.Executor
{
private final java.util.concurrent.BlockingQueue<Callable> queue;
public MyThreadPool(int numThreads) {
queue = new java.util.concurrent.LinkedBlockingQueue<>();
for (int i=0 ; i<numThreads ; i++) {
new Thread(new Runnable(){
@Override
public void run() {
while(true) {
queue.take().call();
}
}
}).start();
}
}
@Override
public <T> Future<T> submit(Callable<T> callable) {
FutureTask<T> future = new FutureTask(callable);
queue.put(future);
return future;
}
public void shutdown(){ }
}
我想不出一种方法来保留线程列表,然后检查它们是否空闲?
最佳答案
您绝对应该持有对您正在创建的线程的引用。比如设置一个字段threads
类型 List<Thread>
并从构造函数中将线程添加到此列表。
之后,您可以实现 shutdown()
在 Thread#join()
的帮助下:
public void shutdown() {
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException e) { /* NOP */ }
}
}
不要忘记替换 while (true)
具有适当的条件(您在 shutdown()
中切换)并考虑使用 BlockingQueue#poll(long, TimeUnit)
而不是 take()
.
编辑:类似于:
public class MyThreadPool implements Executor {
private List<Thread> threads = new ArrayList<>();
private BlockingDeque<Callable> tasks = new LinkedBlockingDeque<>();
private volatile boolean running = true;
public MyThreadPool(int numberOfThreads) {
for (int i = 0; i < numberOfThreads; i++) {
Thread t = new Thread(() -> {
while (running) {
try {
Callable c = tasks.poll(5L, TimeUnit.SECONDS);
if (c != null) {
c.call();
}
} catch (Exception e) { /* NOP */ }
}
});
t.start();
threads.add(t);
}
}
public void shutdown() {
running = false;
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException e) { /* NOP */ }
}
}
// ...
}
关于java - 如何覆盖 executorService 关闭方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064814/