What can be a valid use case to set core pool size to 0 in java 8?

Viewed 239

I am following java concurrency in practice which is based on java 5 and in ThreadPoolExecutor when core pool size is set to 0, the task directly goes to the queue. This is not the case with java 8, even if the core pool size is set to 0, a new thread is created.

ThreadPoolExecutor with corePoolSize 0 should not execute tasks until task queue is full

So my question would be what could be some valid use cases where it makes sense to set core pool size as 0 in java 8, or does it no longer make any sense to do that ?

1 Answers

The odd behavior of ThreadPoolExecutor in Java 5 when the core pool is zero was a bug. It was apparently fixed quietly in Java 6.

Indeed, the problem apparently reappeared in Java 7 (according to https://bugs.openjdk.java.net/browse/JDK-7091003) as a result of some reworking. It was then acknowledged as a bug, and fixed.

Note that Concurrency In Practice describes this as "strange seeming behavior", and doesn't explicitly suggest that you should exploit it in your code.


So what is the actual use-case for setting the corePoolSize to zero?

One such use-case would be to do what Concurrency In Practice suggests in:

Developers are sometimes tempted to set the core size to zero so that worker threads will eventually be torn down, and therefore won't prevent the JVM from exiting.

And by extension, another use-case is if you want the threads to be torn down to reclaim resources ... because the thread pool is unlikely to be used for a long time.

Related