executorservice thread pool_concurrency and threads

Hello everyone, meet again, I am your friend Quanzhanjun.

  • keepAliveTime: Indicates how long the thread will be terminated when there is no task execution. By default, keepAliveTime will work only when the number of threads in the thread pool is greater than corePoolSize, until the number of threads in the thread pool is not greater than corePoolSize, that is, when the number of threads in the thread pool is greater than corePoolSize, if a thread is idle When the time reaches keepAliveTime, it will terminate until the number of threads in the thread pool does not exceed corePoolSize. However, if the allowCoreThreadTimeOut(boolean) method is called, when the number of threads in the thread pool is not greater than corePoolSize, the keepAliveTime parameter will also work until the number of threads in the thread pool is 0;
  • allowCoreThreadTimeOut is false by default, that is, the core thread will not be closed over time, and the thread will be destroyed unless the thread pool is manually closed.
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 10, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy());
        System.out.println("Whether the core thread idle timeout is closed:" + executor.allowsCoreThreadTimeOut());//Whether the core thread idle timeout is closed: false
        for (int i = 0; i < 15; i++) {
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("The number of threads in the thread pool:" + executor.getPoolSize() + ",The number of tasks waiting to be executed in the queue:" +
                    executor.getQueue().size() + ",Number of completed tasks:" + executor.getCompletedTaskCount());
        }
        executor.shutdown();//Manually close the thread pool
        while (!executor.isTerminated()) {
        }
        if (executor.isTerminated()) {
            System.out.println("The number of threads in the thread pool:" + executor.getPoolSize() + ",The number of tasks waiting to be executed in the queue:" +
                    executor.getQueue().size() + ",Number of completed tasks:" + executor.getCompletedTaskCount());
        }
    }
}

class MyTask implements Runnable {
    private int taskNum;

    public MyTask(int num) {
        this.taskNum = num;
    }

    @Override
    public void run() {
        System.out.println("thread name:" + Thread.currentThread().getName() + ",is executing task " + taskNum);
        try {
            Thread.currentThread().sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task " + taskNum + "Finished");
    }
}
copy

The end of the output is: the number of threads in the thread pool: 0, the number of tasks waiting to be executed in the queue: 0, the number of tasks that have been executed: 15

allowCoreThreadTimeOut is set to true, do not close manually

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 10, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy());
        System.out.println("Whether the core thread idle timeout is closed:" + executor.allowsCoreThreadTimeOut());//Whether the core thread idle timeout is closed: false
        executor.allowCoreThreadTimeOut(true);
        for (int i = 0; i < 15; i++) {
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("The number of threads in the thread pool:" + executor.getPoolSize() + ",The number of tasks waiting to be executed in the queue:" +
                    executor.getQueue().size() + ",Number of completed tasks:" + executor.getCompletedTaskCount());
        }
        
        while (true) {
            System.out.println("The number of threads in the thread pool:" + executor.getPoolSize() + ",The number of tasks waiting to be executed in the queue:" +
                    executor.getQueue().size() + ",Number of completed tasks:" + executor.getCompletedTaskCount());
        }
    }
}

class MyTask implements Runnable {
    private int taskNum;

    public MyTask(int num) {
        this.taskNum = num;
    }

    @Override
    public void run() {
        System.out.println("thread name:" + Thread.currentThread().getName() + ",is executing task " + taskNum);
        try {
            Thread.currentThread().sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task " + taskNum + "Finished");
    }
}
copy

The end of the output is: the number of threads in the thread pool: 0, the number of tasks waiting to be executed in the queue: 0, the number of tasks that have been executed: 15

allowCoreThreadTimeOut defaults to false, not manually closed

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 10, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(5), new ThreadPoolExecutor.CallerRunsPolicy());
        System.out.println("Whether the core thread idle timeout is closed:" + executor.allowsCoreThreadTimeOut());//Whether the core thread idle timeout is closed: false
//        executor.allowCoreThreadTimeOut(true);
        for (int i = 0; i < 15; i++) {
            MyTask myTask = new MyTask(i);
            executor.execute(myTask);
            System.out.println("The number of threads in the thread pool:" + executor.getPoolSize() + ",The number of tasks waiting to be executed in the queue:" +
                    executor.getQueue().size() + ",Number of completed tasks:" + executor.getCompletedTaskCount());
        }

        while (true) {
            System.out.println("The number of threads in the thread pool:" + executor.getPoolSize() + ",The number of tasks waiting to be executed in the queue:" +
                    executor.getQueue().size() + ",Number of completed tasks:" + executor.getCompletedTaskCount());
        }
    }
}

class MyTask implements Runnable {
    private int taskNum;

    public MyTask(int num) {
        this.taskNum = num;
    }

    @Override
    public void run() {
        System.out.println("thread name:" + Thread.currentThread().getName() + ",is executing task " + taskNum);
        try {
            Thread.currentThread().sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task " + taskNum + "Finished");
    }
}
copy

The end of the output is: the number of threads in the thread pool: 5, the number of tasks waiting to be executed in the queue: 0, the number of tasks that have been executed: 15

Summarize:

1 If manual shutdown, the number of idle threads is 0 2 If allowCoreThreadTimeOut defaults to false and does not shut down manually, the number of idle threads is the number of core threads

3 If allowCoreThreadTimeOut is set to true and no manual shutdown is performed, the number of idle threads is 0

refer to:

https://blog.csdn.net/u010002184/article/details/80847398 https://www.cnblogs.com/dolphin0520/p/3932921.html Java concurrent programming: the use of thread pools https://blog.csdn.net/lmj623565791/article/details/27250059 Java Concurrency Topic Batch Task Execution with Returned Results CompletionService ExecutorService.invokeAll

Copyright statement: The content of this article is contributed by Internet users, and the opinions of this article only represent the author himself. This site only provides information storage space services, does not own ownership, and does not assume relevant legal responsibilities. If you find any content suspected of infringing/violating laws and regulations on this site, please send an email to report. Once verified, this site will be deleted immediately.

Publisher: Full-stack programmer, please indicate the source: https://javaforall.cn/195375.html Original link: https://javaforall.cn

Tags: html Java Cyber Security https

Posted by isuckat_php on Fri, 30 Sep 2022 15:11:48 +0530