- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个预定的执行程序服务设置,例如
class Test {
private final ScheduledExecutorService executor;
public Test() {
executor = Executors.newSingleThreadScheduledExecutor((runnable) -> {
Thread thread = new Thread(runnable, this.getClass().getName());
thread.setDaemon(true);
return thread;
});
executor.scheduleWithFixedDelay(
new TestRefreshTask(), 0, 50000, TimeUnit.MILLISECONDS
);
}
private class TestRefreshTask implements Runnable {
@Override
public void run() {
//refresh some data
refreshdata();
}
}
public void close() {
executor.shutdown();
try {
if(!executor.awaitTermination(60, TimeUnit.SECONDS)) {
executor.shutdownNow();
executor.awaitTermination(60, TimeUnit.SECONDS));
}
} catch (InterruptedException e) {
//Retry to dispose task
executor.shutdownNow();
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}
为了停止该线程,我调用 close 方法,但该线程通常不会停止。我不确定出了什么问题,以及是否应该以某种方式处理 run() 代码中的 InterruptedException。
最佳答案
来自 shutdownNow()
的 javadoc:
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.
根据经验,线程确实会被中断,但根据本文档,即使这样也不能保证。
但是,您的任务不是您自己控制的,它是在 scheduleWithFixedDelay()
中创建的私有(private)任务。
来自scheduleWithFixedDelay()
的javadoc:
If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor.
这意味着 shutdownNow()
不会停止线程。取消部分对我来说有点不清楚,因为与 Future
相反,Runnable
没有 cancel()
方法。任务停止的另一个原因是执行程序的终止,但是池仅在任务停止时终止。
真正的解决方案隐藏在ScheduledThreadPoolExecutor
的javadoc中的这部分:
By default, such a cancelled task is not automatically removed from the work queue until its delay elapses. While this enables further inspection and monitoring, it may also cause unbounded retention of cancelled tasks. To avoid this, set setRemoveOnCancelPolicy to true, which causes tasks to be immediately removed from the work queue at time of cancellation.
当您命令执行程序停止时,它会设置中断标志,如果它是 ScheduledThreadPoolExecutor
,则在 removeOnCancelPolicy
为 true 时删除任务。
解决方案:
executor.setRemoveOnCancelPolicy(true)
。[编辑]:由于您使用工厂方法,因此无法直接实现。
进行以下更改:
私有(private)最终 ScheduledThreadPoolExecutor 执行器;
executor = new ScheduledThreadPoolExecutor(1, (可运行) -> {
关于java - executorService.shutdownNow() 不会停止线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38826985/
我正在开发一款扑翼应用程序。在出现此错误之前,读取和写入FireStore数据库没有任何问题,但随后突然出现错误(如下所示),并阻止我读取或写入数据库。我一直在寻找答案,但不幸的是,我找不到任何可以解
我正在使用ExecutorService连接任务如下: ExecutorService executor = Executors.newSingleThreadExecutor(); Future f
根据方法 shutdownNow (ExecutorService) 的文档 There are no guarantees beyond best-effort attempts to stop
我正在使用 Executors 作为线程池,并提交任务。 executorService.shutdownNow 是否可以关闭所有任务,即使其中一些任务可能在对数据库或 Socket 的 I/O 调用
我有一个预定的执行程序服务设置,例如 class Test { private final ScheduledExecutorService executor; public Test
我创建了一个 future 任务并将其提交给 Executor。在 run() 方法中,我创建了一个 placementPlanner 对象。此类中的方法调用其他类。但是,当我调用 executor.
我正在运行一个高度并发的 Java 程序。当许多线程向执行程序服务提交任务时,主线程会在某个时刻调用 ExecutorService.shutdownNow()。 执行此操作后,我希望: 执行者服务不
此代码在 future.get() 上永久阻塞。我本以为会出现 CancellationException 或 InterruptedException。谁能解释一下? ExecutorService
正在学习 Java 1.5 中引入的 ExecutorService 以下示例似乎违反了预期行为: public static void main(String[] args) throws Inte
我有一个使用 Executors 创建多个线程的主线程 ExecutorService executor = Executors.newFixedThreadPool(4); 每个线程都有可能运行数小
ExecutorService 有一个方法 List shutdownNow() 哪个 returns a list of the tasks that were awaiting execution
ScheduledExecutorService从 ExecutorService 继承了两个方法, shutdown()和 shutdownNow() .它们的区别: shutdown initia
我有一个表单的处理循环 while (true) { doWork(); Thread.sleep(SLEEP_INTERVAL); } 我想用它制作一个 Runnable,它可以很好
package util.concurrent; import java.util.concurrent.ExecutorService; import java.util.concurrent.Ex
写在开头 面试官:“小伙子,线程池使用过吗,来聊一聊它吧!” 我:“好的,然后巴拉巴拉一顿输出之前看过的build哥线程池十八问...” 面试官满意的点了点头,紧接着问道:“那你知道如何优雅
当其中一个已在运行的任务抛出异常时,我需要取消所有已安排但尚未运行的 CompletableFuture 任务。 尝试了以下示例,但大多数时候 main 方法不会退出(可能是由于某种类型的死锁)。 p
我正在实现一个传输服务器程序,该程序从客户端获取消息(通过控制台输入),然后将其转发到某种邮箱。 为了允许不同客户端同时接收多个消息,我首先创建了一个实现Runnable 接口(interface)的
在第一个进程从 invokeAny 方法提供一些输出后,我立即使用 shutdownNow 关闭进程。但在输出中,即使在调用 shutdownNow() 之后,我也可以看到进程仍在完成流程中并完成其工
执行调用的 ExecutorService.shutdownNow() 后,该类卡在 Future.get() 方法处。我不知道我犯了什么错误。 该类创建固定线程池,并在 5 秒后超时。如果连续发生
我目前有一个 ExecutorService,我想要以下内容: 不应接受任何新任务。 当前任务仍应继续执行。 应返回所有当前排队的任务。 我面临的问题是 shutdown() 不会返回任何未执行的已提
我是一名优秀的程序员,十分优秀!