English 中文(简体)
我首先做什么? 取消计划到期或关闭计划到期执行人服务?
原标题:What do I do first - cancel ScheduledFuture or shutdown ScheduledExecutorService?

我的代码:

ScheduledServiceExecutor service = Executors.newSingleThreadScheduledExecutor();
ScheduledFuture future = service.scheduleWithFixedDelay(
  runnable, 1, 1, TimeUnit.MILLISECONDS
);
// ...
// now it s time to shut it all down
future.cancel(true);
service.shutdown();

我在这里吗?

service.shutdown();
future.cancel(true);

你觉得呢?

最佳回答

在此情况下, 您只需要调用 shutdown ()

scheduledThoreadpoolExportor 中有两个选项, 这是在此幕后创建的 。 get Exeuteexisting Delatined Tasks AfoutShutdown Policy () <() 默认 to trued > 。 因此, 只需关闭服务就会导致取消定期任务, 但任何延迟的任务仍然会执行。 由于此任务是一个定期任务, 关闭时将会取消 。

我的个人意见是,在关闭前最好先手工调用 < code>. cancel( true) ( true) 。 虽然在操作中这没有不同的效果, 但对于可能不知道执行人选项的程序员来说, 检查定期任务在关闭时会被取消是有好处的。 这还意味着如果有人进来, 将执行人更改为在关闭时不会取消的任务, 这个任务仍然会被取消 。 因此, 我认为这里的主要好处是代码清晰 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...