7

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

[edit: the Javadoc for 1.6 is more complete than that for 1.5. See comment below]

Given that:

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

What happens in this case (times are not meant to be absolute, in the real-time sense):

  • at time T, the service kicks off a Runnable task, "task1"
  • at time 2T, task1 has not yet completed, and service is scheduled to fire

Is the service guaranteed to do any of the following?

  • (a) at 2T, kick off a Runnable task, "task2" (recall N = 1)
  • (b) block until task1 is finished
  • (c) skip this time and try again at 3T
  • (d) behavior is undefined

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
Michael Easter
  • 23,733
  • 7
  • 76
  • 107
  • I was looking at the Java 1.5 doc (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate%28java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit%29), which does not have the answer. When writing this question, I linked to the 1.6 doc without looking at it closely. As you'll see, the answer for 1.6 is evident. – Michael Easter Jan 17 '12 at 00:56

2 Answers2

8

The answer is

(b) block until task1 is finished

and that is regardless of number of threads of the executor (task2 might even be not submitted).

The doc says:

If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

(BTW, since there's no intial delay, "task1" will kickoff right away as doc`ed:

executions will commence after initialDelay

).

yair
  • 8,945
  • 4
  • 31
  • 50
1

From the documentation that you linked...

If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

kabuko
  • 36,028
  • 10
  • 80
  • 93