Java的executorservice,处理Java ExecutorService任务中的异常

I'm trying to use Java's ThreadPoolExecutor class to run a large number of heavy weight tasks with a fixed number of threads. Each of the tasks has many places during which it may fail due to exceptions.

I've subclassed ThreadPoolExecutor and I've overridden the afterExecute method which is supposed to provide any uncaught exceptions encountered while running a task. However, I can't seem to make it work.

For example:

public class ThreadPoolErrors extends ThreadPoolExecutor {

public ThreadPoolErrors() {

super( 1, // core threads

1, // max threads

1, // timeout

TimeUnit.MINUTES, // timeout units

new LinkedBlockingQueue() // work queue

);

}

protected void afterExecute(Runnable r, Throwable t) {

super.afterExecute(r, t);

if(t != null) {

System.out.println("Got an error: " + t);

} else {

System.out.println("Everything's fine--situation normal!");

}

}

public static void main( String [] args) {

ThreadPoolErrors threadPool = new ThreadPoolErrors();

threadPool.submit(

new Runnable() {

public void run() {

throw new RuntimeException("Ouch! Got an error.");

}

}

);

threadPool.shutdown();

}

}

The output from this program is "Everything's fine--situation normal!" even though the only Runnable submitted to the thread pool throws an exception. Any clue to what's going on here?

Thanks!

解决方案

From the docs:

Note: When actions are enclosed in

tasks (such as FutureTask) either

explicitly or via methods such as

submit, these task objects catch and

maintain computational exceptions, and

so they do not cause abrupt

termination, and the internal

exceptions are not passed to this

method.

When you submit a Runnable, it'll get wrapped in a Future.

Your afterExecute should be something like this:

public final class ExtendedExecutor extends ThreadPoolExecutor {

// ...

protected void afterExecute(Runnable r, Throwable t) {

super.afterExecute(r, t);

if (t == null && r instanceof Future>) {

try {

Future> future = (Future>) r;

if (future.isDone()) {

future.get();

}

} catch (CancellationException ce) {

t = ce;

} catch (ExecutionException ee) {

t = ee.getCause();

} catch (InterruptedException ie) {

Thread.currentThread().interrupt();

}

}

if (t != null) {

System.out.println(t);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值