ThreadPoolExecutor 中如何实现提交方法吞下异常,但不执行

How does in ThreadPoolExecutor implemented that submit method swallow exception, but execute -not

众所周知,ThreadPoolExecutor 有两种推送任务的方法:

提交并执行。

据我了解 - 主要区别如下:

提交returns Future 如果我们可以观察到调用后任务中发生异常

future.get();

但是如果在作为提交推送的任务中发生异常 - 我们将在控制台中看到这个异常(当然如果这个异常不会明确捕获)

我试图调查 ThreadPoolExecutor 代码并找出它是如何实现的。

我注意到的是,submit 方法在内部使用了 execute 方法:

public Future<?> submit(Runnable task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<Void> ftask = newTaskFor(task, null);
    execute(ftask);
    return ftask;
}

但是我在代码中找不到检测我们是否应该吞下异常的地方。

请帮忙在代码中找到这个逻辑。

你的task是一个FutureTask

如果您查看 run 方法的源代码 (grepcode) :

try {
    result = c.call();
    ran = true;
} catch (Throwable ex) {
    result = null;
    ran = false;
    setException(ex);
}

你看这里Throwable都吞了