Unhandled exceptions with Java scheduled executors

前端 未结 4 1963
情歌与酒
情歌与酒 2020-12-24 13:16

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. E

相关标签:
4条回答
  • 2020-12-24 13:33

    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.

    0 讨论(0)
  • 2020-12-24 13:41

    This man had the same problem.

    http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/

    His solution is to catch Exception inside the runnable and re throw an RuntimeException:

    try {
           theRunnable.run();
        } catch (Exception e) {
           // LOG IT HERE!!!
           System.err.println("error in executing: " + theRunnable + ". It will no longer be run!");
           e.printStackTrace();
    
           // and re throw it so that the Executor also gets this error so that it can do what it would
           // usually do
           throw new RuntimeException(e);
    }
    
    0 讨论(0)
  • 2020-12-24 13:46

    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:

    • define handler at the class of the task which objects are submitted to thread pool;
    • provide your own ThreadFactory implementation to thread pool that initializes default handler via setUncaughtExceptionHandler() or ThreadGroup's uncaughtException();
    0 讨论(0)
  • 2020-12-24 13:59

    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.

    0 讨论(0)
提交回复
热议问题