Exception handling in ThreadPools

后端 未结 6 1838
予麋鹿
予麋鹿 2020-12-23 16:18

I have a ScheduledThreadPoolExecutor that seems to be eating Exceptions. I want my executor service to notify me if a submitted Runnable throws an exception.

For ex

6条回答
  •  有刺的猬
    2020-12-23 17:12

    You could also use a ThreadPoolTaskScheduler from the Spring Framework, which exposes a method to set an error handler and does all the wrapping for you. The default behavior depends on the type of task:

    If the provided ErrorHandler is not null, it will be used. Otherwise, repeating tasks will have errors suppressed by default whereas one-shot tasks will have errors propagated by default since those errors may be expected through the returned Future. In both cases, the errors will be logged.

    If you only want to use the wrapping part and not the TaskScheduler you can use

    TaskUtils.decorateTaskWithErrorHandler(task, errorHandler, isRepeatingTask)
    

    which the TaskScheduler uses internally.

提交回复
热议问题