java futuretask get reject异常,如何捕获FutureTask中的异常

After finding that FutureTask running in a Executors.newCachedThreadPool() on Java 1.6 (and from Eclipse) swallows exceptions in the Runnable.run() method, I've tried to come up with a way to catch these without adding throw/catch to all my Runnable implementations.

The API suggests that overriding FutureTask.setException() should help in this:

Causes this future to report an ExecutionException with the given throwable as its cause, unless this Future has already been set or has been cancelled. This method is invoked internally by the run method upon failure of the computation.

However this method doesn't seem to be called (running with the debugger shows the exception is caught by FutureTask, but setException isn't called). I've written the following program to reproduce my problem:

public class RunTest {

public static void main(String[] args) {

MyFutureTask t = new MyFutureTask(new Runnable() {

@Override

public void run() {

throw new RuntimeException("Unchecked exception");

}

});

ExecutorService service = Executors.newCachedThreadPool();

service.submit(t);

}

}

public class MyFutureTask extends FutureTask {

public MyFutureTask(Runnable r) {

super(r, null);

}

@Override

protected void setException(Throwable t) {

super.setException(t);

System.out.println("Exception: " + t);

}

}

My main question is: How can I catch Exceptions thrown in a FutureTask? Why doesn't setException get called?

Also I would like to know why the Thread.UncaughtExceptionHandler mechanism isn't used by FutureTask, is there any reason for this?

解决方案

setException probably isn't made for overriding, but is provided to let you set the result to an exception, should the need arise. What you want to do is override the done() method and try to get the result:

public class MyFutureTask extends FutureTask {

public MyFutureTask(Runnable r) {

super(r, null);

}

@Override

protected void done() {

try {

if (!isCancelled()) get();

} catch (ExecutionException e) {

// Exception occurred, deal with it

System.out.println("Exception: " + e.getCause());

} catch (InterruptedException e) {

// Shouldn't happen, we're invoked when computation is finished

throw new AssertionError(e);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值