Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ThreadPoolExecutor and Pool Sizes Quirk

DZone's Guide to

ThreadPoolExecutor and Pool Sizes Quirk

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

I recently ran into a problem while using ThreadPoolExecutor. As you probably know, ThreadPoolExecutor takes (among the others), two parameters - corePoolSize and maximumPoolSize. Also by default (and in Java5 you can't change the default) it never stops threads from the "core" pool.

So I've tried to set corePoolSize quite low and raise maximumPoolSize. After some checks I could see that my pool does not behave as expected - I've got very long queue and only core pool threads started. After going deeper into the documentation I've found this:

If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.
And I've used unbounded queue. So, basically, with unbounded queue one should never make maximumPoolSize larger then corePoolSize (at least this won't make any impact - no more then core pool threads will be created).

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}