简体   繁体   English

ScheduledExecutorService调用关闭以进行无限调度

[英]ScheduledExecutorService call shutdown for infinite scheduling

I am using a ScheduledExecutorService that would be scheduled to run every minute infinitely 我正在使用ScheduledExecutorService,该服务计划无限制地每分钟运行

Could some one tell me when to invoke the shutdown on the ScheduledExecutorService 有人可以告诉我何时在ScheduledExecutorService上调用关机吗

I have already looked into Guava MoreExecutors. 我已经研究了番石榴MoreExecutors。 This does not work as I need to block the main thread to keep the other threads running continuously 这不起作用,因为我需要阻塞主线程以保持其他线程连续运行

Use case 用例

I have a task that constantly monitors a system to check for latency/failures. 我的任务是不断监视系统以检查延迟/故障。

I need to run this task periodically and configure Cloudwatch alarms based on it. 我需要定期运行此任务,并基于此任务配置Cloudwatch警报。 Ideally, I would want the task to continue executing even when there is a latency spike, because the alarm would have been triggered 理想情况下,我希望任务即使在出现延迟尖峰时也能继续执行,因为会触发警报

So, I am using a ScheduledExecutorService to periodically schedule the tasks. 因此,我正在使用ScheduledExecutorService定期安排任务。

I want to know the ideal place where I would call shutdown() on the executor service 我想知道在执行程序服务上调用shutdown()的理想位置

Currently, the factory methods in Executors return an instance of ScheduledThreadPoolExecutor 1 . 当前, Executors的工厂方法返回ScheduledThreadPoolExecutor 1的实例。 By default, a ScheduledThreadPoolExecutor will not continue executing periodic tasks after it has been shutdown. 默认情况下, ScheduledThreadPoolExecutor在关闭后将不会继续执行定期任务。 You can configure this via setContinueExistingPeriodicTasksAfterShutdownPolicy . 您可以通过setContinueExistingPeriodicTasksAfterShutdownPolicy进行配置。

Sets the policy on whether to continue executing existing periodic tasks even when this executor has been shutdown . 设置关于即使执行程序已shutdown也是否继续执行现有定期任务的策略。 In this case, executions will continue until shutdownNow or the policy is set to false when already shutdown. 在这种情况下,执行将一直持续到shutdownNow或已关闭的策略设置为false为止。 This value is by default false . 默认情况下,此值为false

As it's really an implementation detail what implementation of ScheduledExecutorService the factory methods of Executors return, it's probably better to create a ScheduledThreadPoolExecutor directly. 因为它是一个真正的实现细节是什么实现ScheduledExecutorService工厂方法Executors返回,它可能会更好打造ScheduledThreadPoolExecutor直接。

ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
executor.scheduleAtFixedRate(() -> System.out.println("Hello, World!"), 0L, 1L, TimeUnit.SECONDS);
executor.shutdown();

Note, this currently configures the ScheduledThreadPoolExecutor to use non-daemon threads. 注意,当前这ScheduledThreadPoolExecutor配置为使用非守护程序线程。 Even though shutdown was called, if the periodic tasks aren't cancelled they will keep the JVM alive. 即使调用了shutdown ,但如果不取消定期任务,它们也会使JVM保持活动状态。 Use a custom ThreadFactory if you want to use daemon threads. 如果要使用守护程序线程,请使用自定义ThreadFactory


1. The newSingleThreadScheduledExecutor methods return a non-configurable wrapper that delegates to a ScheduledThreadPoolExecutor . 1. newSingleThreadScheduledExecutor方法返回一个不可配置的包装 ,该包装将委托给ScheduledThreadPoolExecutor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM