2

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 ?

madcolonel10
  • 735
  • 2
  • 9
  • 22

1 Answers1

3

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • another use-case is if you want the threads to be torn down to reclaim resources -> this should be taken care by using allowCoreThreadTimeOut() right.. do we specifically need to set it to 0 like you mentioned for it take care of this condition ? – madcolonel10 May 26 '20 at 21:10
  • reason for asking is that i am confused by this statement in concurrency in practice "In Java 6, allowCoreThreadTimeOut allows you to request that all pool threads be able to time out;enable this feature with a core size of zero if you want a bounded thread pool with a bounded work queue but still have all the threads torn down when there is no work to do." why does it ask us to do both set corePoolSize to 0 and use allowCoreThreadTimeOut..shouldnt just using allowCoreThreadTimeOut suffice ? – madcolonel10 May 26 '20 at 21:12
  • @madcolonel10 - Assume that you are using a version of Java where the bug has been fixed. Now setting `corePoolSize` to zero is *one way* to achieve the required behaviors; i.e. the 2 use-cases. There is another way to do it too, but the presence of two ways to do something is not necessarily bad. Note that if (for example) they forbade setting corePoolSize to zero, as you seem to be implying, that would *probably* break existing code ... for (IMO) no good reason. – Stephen C May 26 '20 at 23:05
  • 1
    Remember that that textbook is now *fifteen years old* and rather out of date. Some of what it says is no longer the best way to do things. – Stephen C May 26 '20 at 23:08