setException可能不是为了覆盖,而是提供了它以使您可以在需要时将结果设置为异常。您想要做的是重写
done()方法并尝试获取结果:
public class MyFutureTask extends FutureTask<Object> { 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); } }}


