Exception handling in ThreadPools

后端 未结 6 1984
予麋鹿
予麋鹿 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-23 17:15

    I wrote a small post about this problem a while ago. You have two options:

    1. Use the solution provided by Colin Herbert or
    2. use a modified version of Mark Peters solution but instead of assigning a UncaughtExceptionHandler you wrap each submitted runnable into a runnable of your own which executes (calls run) the real runnable inside a try-catch-block.

    EDIT
    As pointed out by Mark, it's important to wrap the Runnable passed to ScheduledExecutorService instead of the one passed to the ThreadFactory.

提交回复
热议问题