How can I dynamically change the thread pool size in a running Spring Boot ExecutorService without restarting the application?

4 weeks ago 32
ARTICLE AD BOX
The documentation for core pool size explains "This setting can be modified at runtime, for example through JMX." You already are safely changing parameters via the recommended approach. There are no specific utilities for this. When we Read the Fine Documentation it explains that getCorePoolSize() will

Return the ThreadPoolExecutor's core pool size.

The ThreadPoolTaskExecutor documentation starts out by explaining that it is a

JavaBean that allows for configuring a ThreadPoolExecutor in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity" properties) and exposing it as a Spring TaskExecutor. This class is also well suited for management and monitoring (for example, through JMX), providing several useful attributes: "corePoolSize", "maxPoolSize", "keepAliveSeconds" (all supporting updates at runtime); "poolSize", "activeCount" (for introspection only).

The default configuration is a core pool size of 1, with unlimited max pool size and unlimited queue capacity. This is roughly equivalent to Executors.newSingleThreadExecutor(), sharing a single thread for all tasks. Setting "queueCapacity" to 0 mimics Executors.newCachedThreadPool(), with immediate scaling of threads in the pool to a potentially very high number. Consider also setting a "maxPoolSize" at that point, as well as possibly a higher "corePoolSize" (see also the "allowCoreThreadTimeOut" mode of scaling).

The first paragraph stresses that those last two are readonly; you might find them of interest for realtime charting of current load. It also encourages you to take advantage of the flexibility of the ConcurrentTaskExecutor interface.

You supplied no source code. You did not tell us if you used a non-default setAllowCoreThreadTimeOut(true) setting to enable dynamic shrinking of the pool during times of low demand.


If this was an XY question, then ask a brand new question and tell us what your true concerns and goals are.

J_H's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article