为什么java ThreadPoolExecutor在发生RuntimeException时会终止线程? [英] Why java ThreadPoolExecutor kill thread when RuntimeException occurs?

查看:319
本文介绍了为什么java ThreadPoolExecutor在发生RuntimeException时会终止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在调用execute方法时会在worker中重新抛出unhanded异常?结果新线程将在下次执行时创建以最大化线程数

Why the unhanded exception is rethrown in worker when calling execute method ? As result new Thread will be created on next execution to maximize threads count

推荐答案


为什么java ThreadPoolExecutor kill发生RuntimeException时的线程?

Why java ThreadPoolExecutor kill thread when RuntimeException occurs?

我只能猜到 ThreadPoolExecutor.execute(...)的原因线程调用 runnable.run() 直接而不是将其包装在 FutureTask中如果您不关心结果,则不会产生 FutureTask 的开销。

I can only guess that the reason why ThreadPoolExecutor.execute(...) has the thread call runnable.run() directly and not wrap it in a FutureTask is so you would not incur the overhead of the FutureTask if you didn't care about the result.

如果你的线程抛出一个 RuntimeException ,这有点是罕见的,并且没有机制将异常返回给调用者,那么为什么要支付包装课?最糟糕的情况是,线程被杀死并将被线程池重新获取并重新启动。

If your thread throws a RuntimeException, which is hopefully a rare thing, and there is no mechanism to return the exception to the caller then why pay for the wrapping class? So worst case, the thread is killed and will be reaped and restarted by the thread-pool.

这篇关于为什么java ThreadPoolExecutor在发生RuntimeException时会终止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆