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

Learn how to build stream processing applications in Java-includes reference application. Brought to you in partnership with Hazelcast.

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

Learn how to build distributed stream processing applications in Java that elastically scale to meet demand- includes reference application.  Brought to you in partnership with Hazelcast.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}