Home > Blockchain >  Can't increase ThreadPoolExecutor's core pool size
Can't increase ThreadPoolExecutor's core pool size

Time:07-25

I'm having issues dynamically increasing the core pool size of a ThreadPoolExecutor in kotlin.

First I define my ThreadPoolExecutor like this:

val POOL = Executors.newFixedThreadPool(20) as ThreadPoolExecutor

Then I add some tasks to the ThreadPoolExecutor.

After some time, in a different thread, while the tasks are still running:

POOL.corePoolSize  = 20

It returns:

Exception in thread "Thread-0" java.lang.IllegalArgumentException
    at java.base/java.util.concurrent.ThreadPoolExecutor.setCorePoolSize(ThreadPoolExecutor.java:1545)
    at MainKt.main$lambda-1(Main.kt:95)
    at java.base/java.lang.Thread.run(Thread.java:833)

The weirdest part is, it works fine if I do this:

POOL.corePoolSize = 40

CodePudding user response:

Turns out, the maximum pool size MUST be greater than or equal to the core pool size as indicated in the setCorePoolSize() method:

if (corePoolSize < 0 || maximumPoolSize < corePoolSize)
    throw new IllegalArgumentException();

Basically instead of:

POOL.corePoolSize  = 20

It should be:

POOL.maximumPoolSize  = 20
POOL.corePoolSize  = 20
  • Related