简体   繁体   中英

Changing Fixed Threadpool max pool size

At the moment I'm creating fixed thread pool using the Executor service like

executor = Executors.newFixedThreadPool(coreAmount);

While this is fine, I was wondering if it's possible to keep the behaviour of creating a number of threads and change the max pool limit so that if all the threads are in use to create a new thread and use it instead of waiting for one of the threads to terminate to start.

So for example if 8 threads are created and are being used, 9th task enters I want it to create a new thread in addition to the 8 currently in use.

It seems newCachedThreadPool() has the behaviour but I also want the ability to create number of threads similar to newFixedThreadPool(int nThreads)

Maybe you can use the ThreadPoolExecutor class. It is an ExecutorService and has the concept of core pool count and max pool count. It also has other features that make it more customizable than the Objects returned by Executors .

Below is an example.

    int coreAmount = 8;

    ExecutorService executor;

    //executor = Executors.newFixedThreadPool(coreAmount);

    int overrun = 4;
    int maxWorkCount = 1_000;
    executor = new ThreadPoolExecutor(coreAmount, coreAmount + overrun, 1, TimeUnit.MINUTES, new ArrayBlockingQueue<>(maxWorkCount));

Here is more info about the params passed in the constructor in the example above.

corePoolSize - the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set

maximumPoolSize - the maximum number of threads to allow in the pool

keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

unit - the time unit for the keepAliveTime argument

workQueue - the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.

Like you said, a cached thread pool is exactly what you're looking for. From it's documentation ,

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources.

(Emphasis: mine)

So for example if 8 threads are created and are being used, 9th task enters I want it to create a new thread in addition to the 8 currently in use.

This is exactly the case with Executors#newCachedThreadPool , as seen from its documentation above.


Here is what you can use if you want to emulate a cached thread pool, but with a minimum amount of 8 threads:

ExecutorService service = new ThreadPoolExecutor(8, Integer.MAX_VALUE,
                                                 60L, TimeUnit.SECONDS,
                                                 new SynchronousQueue<Runnable>());

This is available in the ThreadPoolExecutor using Core Pool & Maximum Pool size values: From the javadoc , you can see that:

If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool.

By setting maximumPoolSize to an essentially unbounded value such as Integer.MAX_VALUE, you allow the pool to accommodate an arbitrary number of concurrent tasks .

Most typically, core and maximum pool sizes are set only upon construction, but they may also be changed dynamically using setCorePoolSize(int) and setMaximumPoolSize(int).

So, in your example, you would need to set the 'corePoolSize' to the value of 8. You would then need to set the 'maximumPoolSize' which would then serve as the upper-bound to the pool. Also, as in the javadoc, these values can be altered dynamically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM