Java 执行器有一个线程池。如果我有固定长度的池(例如 8)。如果应用程序不终止,这 8 个线程是否会永远存在?如果部分代码抛出异常,池中的线程会死掉吗?
另一方面,如果我想终止线程,如何终止线程?
最佳答案
是的,只要 ExecutorService
存在,它们就会永远存在。您可以通过调用 ExecutorService.shutdown()
关闭它们。至于你问题的另一部分,javadoc解释一切:
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.
关于Java线程生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724417/