Java worker threads sum

I am using ExecutorService to create a fixed pool of threads and start multiple worker threads that listen to something and then do their job. However, sometimes I would like to increase or decrease the number of threads that are running at the same time to fine tune the performance of the application (without restarting the application or killing any current threads). Should I create my own thread pool or is there a way to have a resizable pool that will handle workers start / stop as needed.

+3


source to share


3 answers


However, sometimes I would like to increase or decrease the number of threads that are running concurrently to fine tune the performance of the application (without restarting the application or killing any threads currently running).

If you mean dynamically changing with some tool or something, then I'm not sure, but you can put some code logic to manage.

You can use java.util.concurrent.ThreadPoolExecutor

and use the properties CorePoolSize

and MaxPoolSize

to have control of the thread pool .

corePoolSize and maximumPoolSize:

  • ThreadPoolExecutor automatically adjusts the pool size (see getPoolSize ()) according to the limits set by corePoolSize (see getCorePoolSize ()) and maximumPoolSize (see getMaximumPoolSize ()).
  • When a new task is dispatched when the method (java.lang.Runnable) is running and fewer corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle.
  • If there are more than corePoolSize but fewer threads MaximumPoolSize, a new thread will be created only if the queue is full.

However, before you decide, I would recommend reading below the excerpt from ThreadPoolExecutor Java docs .

However, programmers are strongly encouraged to use the more convenient Executors factory methods Executors.newCachedThreadPool () (unlimited thread pool, with automatic thread recuperation ), Executors.newFixedThreadPool (int) (fixed size thread pool) and Executors.newSingleThreadExecutor () (single background thread). which preconfigure the settings for the most common use cases.




Example code:
See below for example code. You can figure most of the things by reading the code and looking at the Java docs. However, what may not be obvious is that

  • we used ArrayBlockingQueue

    to get a limited queue of 20 capacities (you can define the queue capacity as per your requirement). So, as soon as there are more than 20 tasks waiting in the queue, new threads will be created, but up to the maxPoolSize maximum .
  • Depending on the load, we increase the number of kernel pool threads, which means that more threads will handle your tasks, so the chances of task queues being executed are less. But you can also play with maxPoolSize.

You can read the "Queuing" section of ThreadPoolExecutor and decide on some other queue as per your requirement.

ThreadPoolExecutor.setCorePoolSize (int)
Sets the base number of threads. This overrides any value set in the constructor. If the new value is less than the current value, the excess of existing streams will be stopped when they become free. If more, new threads will be launched as needed to perform any tasks in the queue .

    //Read Java docs for details about construcutor...
    ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 100, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(20));
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //Do your task...
        }
    };

    executeTasks(poolExecutor, runnable, false, false); //Compute last 2 parameters as you need and pass on required values.

public static void executeTasks(ThreadPoolExecutor poolExecutor, Runnable runnable, boolean isUnderLoad, boolean isOverLoad){
    if(isOverLoad && isUnderLoad){
        //Handle this situation, this should not be allowed, probably a coding bug can result this...
    }
    poolExecutor.submit(runnable);
    if(isUnderLoad){
        poolExecutor.setCorePoolSize(5);
    }
    if(isOverLoad){
        poolExecutor.setCorePoolSize(20);
    }
}

      

+3


source


ThreadPoolExecutor

lets you do it. See setCorePoolSize and setMaximumPoolSize



+3


source


ThreadPoolExecutor dynamically increases and decreases the number of threads. Make sure you set the pool size correctly. CorePoolSize determines the maximum number of threads to spawn. MaxPoolSize comes into play when the BlockingQueue has a finite size. When you have a finite queue size, new threads are created for each new task assigned until it reaches the kernel size. If the number of requests in queue increments is beyond the final queue size, new threads are spawned until MaxPoolSize is reached.

+1


source







All Articles