ExecutorService executor = ****;
List<List<Object>> objectGroup = Lists.partition(objectList, 5);
for (List<Object> eachGroup : objectGroup ) {
CountDownLatch latch = new CountDownLatch(eachGroup.size());
for (Object obj: eachGroup) {
executor.submit(() -> {
doTask(obj);
latch.countDown();
});
}
try {
if (!latch.await(15, TimeUnit.MINUTES)) {
// todo close all thread in executor
}
}
catch (InterruptedException e) {
System.out.println();
}
}**
我想在15分钟后关闭executor中的所有线程,但是我不知道如何关闭。
有人说要使用executor.shutDownNow(),但是它会关闭执行器(我不能再提交线程了),我不希望这种情况发生
**
发布于 2022-11-02 10:06:40
在java中阻止Thread的方法只有一种:让它这样做。
new Runnable() {
// This status should be checked repeatedly within a process
if(Thread.interrupted()) {
// Executore has probably ask to stop
}
}此外,您还有一个ExecutorService实例。要让所有线程停止,您应该调用shutDownNow()方法。此方法将将interrupted标志设置为ExecutorService中的所有线程。
P.S.看上去像是贴子给出了细节。
https://stackoverflow.com/questions/74287306
复制相似问题