For Java's ScheduledExecutorService's scheduleAtFixedRate method does it return 1 ScheduledFuture or many?

huangapple go评论79阅读模式
英文:

For Java's ScheduledExecutorService's scheduleAtFixedRate method does it return 1 ScheduledFuture or many?

问题

因为有一个可以多次执行的命令,所以我提出了这个问题。它在每次执行命令时是否都会返回一个ScheduledFuture,还是在某个时候会返回一个单独的ScheduledFuture?

英文:

I ask this question because there is a command that executes multiple times. Does it return a ScheduledFuture every time the command executes or does it return a single ScheduledFuture at some point?

答案1

得分: 4

仅在安排“Runnable命令”时返回单个ScheduledFuture<?>

根据JavadocscheduleAtFixedRate 返回

> 一个代表重复任务系列待完成的ScheduledFuture

您还可以在方法签名中看到:

ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

“Runnable命令”将根据配置的间隔继续执行,无需进一步干预。

> 任务执行的序列持续无限期,直到发生以下异常完成之一:
>
> - 通过返回的future明确取消任务。
> - 执行程序终止,也会导致任务取消。
> - 任务的执行抛出异常。

通过在Future上调用cancel来通过编程方式终止Runnable命令的所有未来计划执行,scheduleAtFixedRate返回单个ScheduledFuture提供了有用的机制。

英文:

Only a single ScheduledFuture<?> is returned upon scheduling the Runnable command.

According to the Javadoc, scheduleAtFixedRate returns

> a ScheduledFuture representing pending completion of the series
> of repeated tasks

You can also see this in the method signature:

ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

The Runnable command continues to be executed according to the configured interval without further intervention.

> The sequence of task executions continues indefinitely until one of
> the following exceptional completions occur:
>
> - The task is explicitly cancelled via the returned future.
> - The executor terminates, also resulting in task cancellation.
> - An execution of the task throws an exception.

Having a single ScheduledFuture returned by scheduleAtFixedRate provides a useful mechanism to programmatically discontinue all future scheduled executions of Runnable command through a call to cancel on the Future.

huangapple
  • 本文由 发表于 2020年7月24日 03:45:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63062013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定