英文:
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<?>
。
根据Javadoc,scheduleAtFixedRate
返回
> 一个代表重复任务系列待完成的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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论