[concurrency-interest] ScheduledExecutor execute takes longer thanthe next job.. what is going to happen?

David Holmes dholmes at dltech.com.au
Wed Sep 7 03:12:24 EDT 2005


Peter,

> What happens if the ScheduledExecutor executes a heavy job,
> only has a single thread, and the next job needs to be scheduled?
>
> If the execution of a job takes 1 minute.. and every 30 seconds
> a job should be executed.. are the jobs that have to wait dropped?
> Stored? Can this be controlled (maybe with timeouts)?

As per the documentation: "Delayed tasks execute no sooner than they are
enabled, but without any real-time guarantees about when, after they are
enabled, they will commence."

If there are no threads available to process it then it will wait until a
thread is available. (The tasks are put into a work queue and only come
out - excluding cancellation - if a worker thread asks for a task, and the
task is eligible to execute.)


scheduleWithFixedDelay states:

"Creates and executes a periodic action that becomes enabled first after the
given initial delay, and subsequently with the given delay between the
termination of one execution and the commencement of the next."

So the next occurrence of the task won't execute until "delay" timeunits
after the previous invocation completed.

scheduleAtFixedRate is less clear on how it behaves. I believe the basic
model for periodic tasks is that at the end of each execution the task looks
to see if its period has elapsed since its last "start" time and if so it is
eligible to run again. This means that you'll never have multiple executions
of the one task happening concurrently even if multiple periods have passed.
But I don't know if it will execute again immediately or have to wait its
turn if others tasks could also execute.

Cheers,
David Holmes



More information about the Concurrency-interest mailing list