简体   繁体   中英

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

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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