相关疑难解决方法(0)

ThreadPools中的异常处理

我有一个ScheduledThreadPoolExecutor似乎正在吃Exception.如果提交的Runnable抛出异常,我希望我的执行程序服务通知我.

例如,我希望下面的代码至少打印出IndexArrayOutOfBoundsException的stackTrace

threadPool.scheduleAtFixedRate(
  new Runnable() {
    public void run() {
      int[] array = new array[0];
      array[42] = 5;
    }
  },
  1000,
  1500L,
  TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)

作为一个附带问题.有没有办法为ScheduledThreadPoolExecutor编写一般的try catch块?

//////////原始问题的结尾//////////////

正如所建议的那样,以下装饰器运行良好

public class CatcherTask implements Runnable{

    Runnable runMe;

    public CatcherTask(Runnable runMe) {
        this.runMe = runMe;
    }

    public void run() {
        try {
            runMe.run();
        } catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java exception-handling threadpool

42
推荐指数
3
解决办法
2万
查看次数

标签 统计

exception-handling ×1

java ×1

threadpool ×1