Java

Java

Made by DeepSource

Maximum pool size of ScheduledThreadPoolExecutor cannot be changed JAVA-W0012

Anti-pattern
Major

It is not possible to change the max pool size of a ScheduledThreadPoolExecutor using the setter functions inherited from ThreadPoolExecutor.

From ScheduledThreadPoolExecutor's JavaDocs:

While ScheduledThreadPoolExecutor inherits from ThreadPoolExecutor, a few of the inherited tuning methods are not useful for it. In particular, because it acts as a fixed-sized pool using corePoolSize threads and an unbounded queue, adjustments to maximumPoolSize have no useful effect.

This may be contrary to assumptions the programmer may make, leading to subtle logic errors.

Bad Practice

ScheduledThreadPoolExecutor ste = new ScheduledThreadPoolExecutor(2);
ste.setMaximumPoolSize(4); // Doesn't work

Recommended

Check how ScheduledThreadPoolExecutor is used and rewrite the code to obviate the need to change the max pool size.

If it is possible to avoid the need to resize the thread pool, try that approach.

References