我遇到了一个关于ThreadPoolExecutor.
写了一些代码后,发现submit()方法会吃掉RuntimeException程序抛出的,但是execute()方法会重新抛出RuntimeException`。我想知道这其中的原因。
最近看了一个线程池的源码ThreadPoolExecutor,知道了线程池的原理。现在我明白了execute()方法是如何执行的,但我不明白submit()方法是如何执行的。我只知道该submit()方法会将Runnableor包装Callable在 a 中FutureTask并调用该execute()方法:
public Future submit(Runnable runnable)
{
if(runnable == null)
{
throw new NullPointerException();
} else
{
RunnableFuture runnablefuture = newTaskFor(runnable, null);
execute(runnablefuture);
return runnablefuture;
}
}
所以,我的问题是:如何ThreadPoolExecutor执行FutureTask以及为什么被RuntimeException吃掉?