Java changes the number of worker threads

I'm using executorservice to create a fixed thread pool and start several working threads. These threads listen for what's happening and then finish their work

Solution

If you mean using some tool or something to change dynamically, I'm not sure, but you can set up some code logic to control it

You can use Java util. concurrent. ThreadPoolExecutor and use the corepoolsize and maxpoolsize properties to control the thread pool

Corepoolsize and maximumpoolsize:

>ThreadPoolExecutor will automatically resize the pool (see getpoolsize()) based on the boundaries set by corepoolsize (see getcorepoolsize()) and maximumpoolsize (see getmaximumpoolsize()). > When a new task is submitted in the method execute (Java. Lang. runnable) and fewer threads are running than corepoolsize, a new thread will be created to process the request even if other worker threads are idle. > If there are more than one corepoolsize but fewer than one maximumpoolsize thread is running, a new thread will be created only when the queue is full

But before you decide, I suggest you read the following Java document from ThreadPoolExecutor

Code example: please refer to the following code example You can understand most things by reading the code and browsing the Java documentation However, the facts may not be obvious

>We use arrayblockingqueue to obtain 20 capacity bounded queues (you can determine the queue capacity as needed) Therefore, once there are more than 20 tasks waiting in the queue, a new thread will be created, but up to maxpoolsize. > Based on the load, we have increased the number of threads in the core pool, which means that more threads will process your tasks, so there are fewer opportunities for tasks to queue But you can also use maxpoolsize

You can read the "queue" section of ThreadPoolExecutor and determine other queues according to your requirements

ThreadPoolExecutor. Setcorepoolsize (int) sets the number of core threads This overrides any value set in the constructor If the new value is less than the current value, the redundant existing thread will be terminated when the next idle If larger is needed, the new thread will start to perform any queued tasks

//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);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>