swarbrick85
swarbrick85

Reputation: 81

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?

Upvotes: 1

Views: 512

Answers (1)

Philip Wrage
Philip Wrage

Reputation: 1559

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.

Upvotes: 4

Related Questions