ScheduledExecutorService

ScheduledExecutorService

从这里的文档:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html#scheduleAtFixedRate(java.lang.Runnable,%20long,%20long,%20java.util.concurrent.TimeUnit)

它说如下:



是否有任何等效的ScheduledExecutorService.scheduleAtFixedRate在未捕获的异常后不会关闭?

最佳答案

正如安迪·特纳(Andy Turner)所提到的,您可以使用“包罗万象”包装器:

public class CatchAllRunnable implements Runnable{
     private final Runnable wrappee;

     public CatchAllRunnable( Runnable r ){ wrappee = r; }

     @Override
     public void run(){
         try{ wrappee.run(); }
         catch( Exception e ){ /* LOG e */ }
     }
}

然后安排:
yourScheduledExecutorService.scheduleAtFixedRate( new CatchAllRunnable(originialRunnable), 1, TimeUnit.Minute);

时间当然会根据您的要求进行调整。

关于java - ScheduledExecutorService不会因未捕获的异常而停止线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32480075/

10-10 01:41