Basic understanding and use of thread pool

Thread Pool

  • Pooling ideas: thread pool, string constant pool, database connection pool
  • Can improve resource utilization

How the thread pool works:

  1. Create multiple thread objects in advance and put them in the thread pool.

  2. When a task comes in, the thread pool will assign a thread to the task, and then put the thread back into the thread pool after the execution is completed.

  3. When the number of tasks is more than the number of threads in the thread pool, the tasks not allocated to the thread will enter the waiting queue for waiting (the task entry and exit of the waiting queue is first in, first out, last in, last out), and when the thread is used up and released into the thread pool, Then obtain threads from the thread pool for use by tasks in the waiting queue.

  4. When the waiting tasks in the waiting queue reach the limit of the waiting queue, the thread pool will create a new thread for the tasks overflowing outside the queue.

  5. The threads in the thread pool have reached the upper limit, there are no free thread objects, and the tasks in the waiting queue are also full, and there are task overflows, which will trigger the rejection strategy of the thread pool. In this way, thread resources can be reused

advantage:

  • Improve thread utilization
  • Improve the response speed of the program Thread objects are created in advance and will not be destroyed after use, avoiding the performance consumption of thread creation and destruction, thereby improving the corresponding speed of the program
  • Facilitate unified management of thread objects
  • Can control the maximum number of concurrency

The traditional way of creating threads

  • Manually create thread objects
  • perform tasks
  • After execution, release the thread object

线程利用率比较低

The thread pool creates threads using

  1. Create a thread pool and configure corresponding parameters (there are many ways to create a thread pool, which will be discussed later.)

insert image description here
Number of core threads: the number of threads that basically exist in the thread pool

The maximum capacity of the thread pool: the core threads in the thread pool are all executed tasks, and the waiting queue is full, and the maximum capacity of the thread pool after creating new threads is expanded

Thread survival time: the interval between the temporary thread not being called and being destroyed

Time unit: the unit of thread survival time

Thread creation factory: specifies the factory that creates threads

Rejection strategy: When the thread pool reaches the maximum capacity and the waiting queue is full, how to deal with redundant threads.

  1. After the thread is created, use the for loop to simulate the execution of the task, and finally close the thread pool
  ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                5,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for (int i = 0; i < 6; i++){
    
    
            threadPoolExecutor.execute(() -> {
    
    
                System.out.println(Thread.currentThread().getName() + "=======> 执行 ");
            });
        }
        threadPoolExecutor.shutdown();
  1. Passed multiple tests:
    when 执行任务<=核心线程数+等待队列容量the thread pool only uses core threads to process tasks, no temporary threads will be created, and
    insert image description here
    when 核心线程数+等待队列容量<执行任务<=线程池最大容量+等待队列容量the time is right, the thread pool will create temporary threads to handle overflowing programs outside the waiting queue.
    insert image description here
    When 执行任务>线程池最大容量+等待队列容量, that is, when the thread pool cannot handle the overflow program, the thread pool will execute the rejection strategy
    insert image description here

Guess you like

Origin blog.csdn.net/weixin_58286934/article/details/129280725