Java Behavior ScheduledExecutorService.scheduleAtFixedRate ()

I have a question regarding the scheduleAtFixedRate () method in ScheduledExecutorService in Java 6.

[ edit : Javadoc for 1.6 is more advanced than for 1.5. See Comment below]

Given that:

  • ScheduledExecutorService built with N = 1 thread in the pool
  • fixed rate is a period of T seconds
  • no initial delay

What happens in this case (times should not be absolute, in real time):

  • at time T, the service starts the Runnable task, "task1"
  • at time 2T, task 1 is not yet completed, and the service should start

Is the service guaranteed for any of the following?

  • (a) in 2T, run Runnable task, task2 (remember N = 1)
  • (b) block until task 1 is completed
  • (c) skip this time and try again at 3T
  • (d) undefined behavior

Or something else? Does the answer change if N> 1?

+6
source share
2 answers

Answer

(b) block until task 1 is completed

and this is independent of the number of worker threads (task2 can even be sent).

Doc says:

If any execution of this task takes longer than its period, subsequent executions may begin late , but will not be performed simultaneously .

(BTW, since there are no delays, "task1" will immediately start as doc`ed:

execution will begin after initialDelay

)

+6
source

From the documentation you linked ...

If any execution of this task takes longer than its period, subsequent executions may be delayed, but will not be performed simultaneously.

+1
source

Source: https://habr.com/ru/post/906149/


All Articles