views:

625

answers:

3

Hi,

I have the following issue and I would like to know what exactly happens. I am using Java's ScheduledExecutorService to run a task every five minutes. It works very well. Executors completely changed the way I do thread programming in Java.

Now, I browsed Java Doc for information about what would be the behavior in case the scheduled task fails with an unhandled exception, but couldn't find anything.

Is the next scheduled task still going to run? If there is an unhandled exception, the scheduled executor stops scheduling task? Can anyone point to information regarding this simple issue?

Thanks a lot.

+4  A: 

The Javadoc of both scheduleAtFixedRate and scheduleWithFixedDelay says "If any execution of the task encounters an exception, subsequent executions are suppressed." I don't find that to be exactly crystal clear, but it seems to be saying that if your run method throws any kind of exception, then the scheduler will effectively drop that task. Any other tasks running via that scheduler should not be affected. It shouldn't be hard to test what it actually does...

Cancellation of the task may not necessarily be a bad thing. If the run method throws a RuntimeException, it's probably got a bug somewhere, and the state of the system is unknown. But at minimum I would advise catching RuntimeException in your run method, and logging the full stack trace at SEVERE. You may want to then rethrow to cancel the task, depending on the circumstances. But either way you'll need the logging to have a fighting chance of working out what went wrong.

Lachlan
Thanks a lot for the response. I didn't see that in API Doc.
Pablo Santa Cruz
A quick browse of the source for ThreadPoolExecutor (atop which the STPE is implemented) suggests that RuntimeExceptions will be neatly caught and reported back via the ScheduledFuture, but that they will also cause the worker thread to terminate. The TPE will notice this worker thread termination and spin up a new replacement, if necessary. See runWorker() and processWorkerExit() in java.util.concurrent.ThreadPoolExecutor in the JDK sources, or on Doug Lea's site.
andersoj
+2  A: 

If you are using scheduleAtFixedRate() or scheduleAtFixedDelay(), and your task bails out with an exception, that task will not be rescheduled. However, other independent tasks should continue to execute as expected. (See API Docs). If you care that this has happened, you can grab ahold of the ScheduledFuture that is returned and call the get() method. If the underlying task tosses an exception, you'll get it thrown out of the get() method, wrapped in an ExecutionException.

andersoj
Thanks a lot for the response. +1. Too bad I can't accept two answers as "the answer". Thanks again.
Pablo Santa Cruz
Hey, man, Lachlan beat me to the punch... rules is rules. ;-)
andersoj
A: 

It looks like the API doesn't define any specific exception handling mechanism. I.e. uncaught exception just pops through the thread frames and eventually is being logged to stderr.

I see that you can exploit the following exception handling strategies:

denis.zhdanov