Are dead threads replaced in an ExecutionContext and/or Java thread pool?
Asked Answered
H

1

5

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.

For example, if I create an ExecutionContext wrapping a FixedThreadPool(100), if one thread dies, does he thread pool replace the thread?

Hesitate answered 19/6, 2016 at 16:44 Comment(2)
docs.oracle.com/javase/8/docs/api/java/util/concurrent/…Etz
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.Dolt
A
9

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."

Ailment answered 19/6, 2016 at 17:52 Comment(2)
will this new thread execute the task from first or continue where dead thread has stopped?Bartlet
The new thread will execute the task afresh. If you want to continue from where the task was left by previous dead thread, then you must put such logic in code by yourself.Compliance

© 2022 - 2024 — McMap. All rights reserved.