Unhandled exceptions with Java scheduled executors

前端 未结 4 2079
情歌与酒
情歌与酒 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: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);
    }
    

提交回复
热议问题