[concurrency-interest] Should old ForkJoinWorkerThread die if starting a new thread fails?

Doug Lea dl at cs.oswego.edu
Tue Jun 6 18:59:48 EDT 2017


On 06/06/2017 11:32 AM, Jarkko Miettinen wrote:

> In any case, currently if starting a new thread in
> ForkJoinPool#createWorker fails with an exception (OutOfMemoryError
> being the most common),  the thread that tries to start that new thread
> dies too.

You can catch the exception, and try to cope.
One possibility is to try to help execute tasks that might
not otherwise be run. As in:

try {
   task.fork(); // or
   pool.submit(task); // or similar
} catch (Exception ex) {
   ForkJoinTask.helpQuiesce(); // or
   pool.awaitQuiescence(1, SECONDS); // help run tasks for at most 1 sec
}

(You could then encasulate this as a helper method.)

If the failure is a memory-based OOME, then this might also fail.
And in any case won't execute tasks  asynchronously using the same
scheduling. But it does provide a best-effort fallback.

> I think OutOfMemoryError is generally be considered something that
> should not be recovered from. 

As implied in other replies, one reason for not retrying here
(also ThreadPoolExecutor) is to help avoid infinitely cascading
(and sometimes silent) failures, for which we (in j.u.c) have no
recourse.

-Doug



More information about the Concurrency-interest mailing list