gpt4 book ai didi

java - ThreadPoolExecutor 创建的线程在关闭后仍保持运行

转载 作者:行者123 更新时间:2023-12-01 17:51:12 25 4
gpt4 key购买 nike

我正在尝试使用两个 threadPoolExecutor 应用某些数据的多线程生产者/消费者的原理,我注意到一切正常,但程序拒绝关闭,并且在关闭 2 个 threadPools 后,一些线程仍在运行:

在主类中:

ExecutorService executor = new ThreadPoolExecutor(10, 10, 40, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(edmsIds.size()));
ExecutorService consumerExecutor = new ThreadPoolExecutor(10, 10, 0L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(edmsIds.size()));for(
String edmsID:edmsIds)

{

Runnable worker = new BatchWorkerThread(resumeFile, stratrKeysToUpdate, xslFiles, errorThrown, edmsID,
consumerExecutor, edmsIds.size(), executor);
executor.execute(worker);
}

在生产者类别中:

while (edmsNumber > 0) {
Runnable consumer = new ConsumerThread(brokerWorker);
consumExecutor.execute(consumer);//decreasing the edmsNumber

}
if (edmsNumber < 1) {
prodExecutor.shutdownNow();
consumExecutor.shutdownNow();
}

最佳答案

请参阅doc :

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.

如果正在运行的任务不可中断(不响应中断信号),则继续执行。

关于java - ThreadPoolExecutor 创建的线程在关闭后仍保持运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49939508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com