在 ExecutionContext 和/或 Java 线程池中是否替换了死线程? [英] Are dead threads replaced in an ExecutionContext and/or Java thread pool?
问题描述
当一个线程因异常而死亡时,这个线程会发生什么?如果它在线程池中,它是否会产生一个新线程?我对 scala ExecutionContext 中发生的事情很感兴趣,但由于 ExecutionContext 包装了一个 java 线程池,我认为 Java 用户也会知道答案.
When a thread dies due to an exception, what happens to this thread? If it is inside a thread pool, does it spawn a new thread? I'm interested in what happens in scala ExecutionContext, but since an ExecutionContext wraps a java thread pool, I think that Java users will also know the answer.
例如,如果我创建一个包装了 FixedThreadPool(100) 的 ExecutionContext,如果一个线程死亡,他的线程池会替换该线程吗?
For example, if I create an ExecutionContext wrapping a FixedThreadPool(100), if one thread dies, does he thread pool replace the thread?
推荐答案
线程本身在死后不能产生新线程,但是线程池可以替换它.例如,Executors.newFixedThreadPool()
创建的线程池在需要时替换死线程.来自 Executors.newFixedThreadPool()
的文档:
The thread itself cannot spawn a new thread after it dies, however the thread pool can replace it. For example, the thread pool created by Executors.newFixedThreadPool()
replaces dead threads when needed. From the documentation for Executors.newFixedThreadPool()
:
"如果任何线程在执行之前由于失败而终止关闭,如果需要执行,一个新的将取代它后续任务."
"If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks."
这篇关于在 ExecutionContext 和/或 Java 线程池中是否替换了死线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!