I have used the thread pool in the 2023 interview series and I don’t know the underlying principles. It’s nothing!

1. Why use thread pool:


1. Reduce the performance consumption caused by frequent thread creation and destruction

2. Easy to manage threads

3. Improve response speed (no need to wait for thread creation)

4. Can provide more functions

2. How to create a thread pool:


Created by Executors

Created through ThreadPoolExecutor (advocated)

3. Thread pool parameter description:


corePoolSize

MaxinumPoolSize

keepAliveTime

TimeUint unit

BlockingQueue<Runnable> workQueue,

RejectedExecutionHandler handler

4. Thread pool process:


-When there is a task, if the current number of threads is less than the number of core threads, then the number of core threads will be created first to execute the task.

When the number of threads has reached the number of core threads, the task is added to the blocking queue. When the blocking queue is full, if the current number of threads is greater than the number of core threads and less than the maximum number of threads, a number of non-core threads will be created for execution. If not satisfied, execute rejection policy

Thread pool status:

Running-ShowDown-ShowDownNow-Stop-Tidying-Terminated

5. Several important methods of thread pool to understand the underlying principles


-excute(Runnable command)
-addWorker(Runnable fistTask,boolean core)


summary:


The --addWorker method has three different parameter calls. In the process of adding tasks, the thread pool status and number of threads are judged multiple times.

addWorker(first, true): less than the number of core threads, create core threads to perform tasks

addWorker(null, false): Add to the blocking queue, the number of threads < the maximum number of threads

addWorker(first, false): The blocking queue is full, create a new non-core thread to perform the task

-runWorker(Worker w)
-getTask()
-processWorkerExit(Worker w,boolean completedAbruptly)

6. Thread pool technology highlights:


The thread pool uses CAS and AutomicInteger to change the number of threads and thread status. The core parameters are modified with Volatile. At the same time, Condition and ReentrantLock are used to create threads, changes in the thread pool status, changes in the number of work tasks (adding and deleting), etc. , to determine whether to use poll or take method or task through allowCoreThreadTimeOut || wc > corePoolSize. Worker also inherits AQS (non-reentrant, the thread will not be interrupted when running)

7. Points to note when using thread pools:


1. Properly set core threads and the maximum number of threads to make full use of resources while avoiding waste of resources.

2. Choose a rejection strategy and pay attention to handling exceptions thrown.

3. Unreasonable setting of blocking queue, using LinkedBlockedQueue leads to thread pool overflow

4. Repeat the creation of the thread pool, remember to showdown

5. According to business needs, set allowCoreThreadTimeOut (true) to recycle blocked core threads

8. Simple custom thread pool name: distinguish thread pools under different businesses


9. Simple custom rejection policy

10. Leave it to small tasks

The blocking queue is full, what should I do?
 

Guess you like

Origin blog.csdn.net/weixin_42450130/article/details/132645241