当前位置:网站首页>[advanced concurrency] - thread pool summary
[advanced concurrency] - thread pool summary
2022-06-11 07:04:00 【I will definitely go to Maidang in rainbow sea】
️ Personal website :code treasure , Welcome to visit
My public number :code treasure , Share your learning resources , Welcome to your attention
Thank you very much for your support and praise
List of articles
- What is a thread pool ?
- There are several ways to create thread pools ?
- ThreadPoolExecutor Constructor important parameter analysis
- Use ThreadPoolExecutor
- The operation process of thread pool
- States in the thread pool
- ThreadPoolExecutor Refusal strategy
- How to terminate a thread pool ?
- In the thread pool submit() and execute() What's the difference ?
What is a thread pool ?
Thread pool Provides a way to limit and manage resources ( Including performing a task ) The way . Every Thread pool And maintain some basic statistics , For example, the number of tasks completed .
Borrow here 《Java The art of concurrent programming 》 Let's talk about it The benefits of using thread pools :
- Reduce resource consumption . Reduces the cost of thread creation and destruction by reusing created threads .
- Improve response time . When the mission arrives , Tasks can be executed immediately without waiting for a thread to be created .
- Improve the manageability of threads . Threads are scarce resources , If unlimited creation , Not only does it consume system resources , It also reduces the stability of the system , Uniform allocation is possible using thread pools , Tune and monitor .
There are several ways to create thread pools ?
Use Executors Tool class creates thread pool
Executors Provides a series of factory methods for creating thread pools , The returned thread pool is implemented ExecutorService Interface .
There are mainly newSingleThreadExecutor,newFixedThreadPool,newCachedThreadPool,newScheduledThreadPool
newFixedThreadPool: Thread pool with fixed number of threads .corePoolSize = maximumPoolSize,keepAliveTime by 0, Work queues use unbounded
newSingleThreadExecutor: Thread pool with only one thread .corePoolSize = maximumPoolSize = 1,keepAliveTime by 0, Work queues use unbounded
newCachedThreadPool: Create a thread pool for new threads as needed . The number of core threads is 0, The maximum number of threads is Integer.MAX_VALUE,keepAliveTime by 60 second , Work queues use synchronous handover SynchronousQueue. The thread pool can be expanded infinitely , As demand increases , You can add new threads , When the demand decreases, idle threads will be automatically recycled . Suitable for performing many short-term asynchronous tasks , Or a lighter load server .
newScheduledThreadPool: Create a thread pool that performs tasks in a delayed or timed manner , Work queue is DelayedWorkQueue. Suitable for periodic tasks that require multiple background threads to execute .
Executors The disadvantages of each method :
newFixedThreadPool and newSingleThreadExecutor: The main problem is that the heap of request processing queues can consume a lot of memory , even to the extent that OOM.
newCachedThreadPool and newScheduledThreadPool: The main problem is that the maximum number of threads is Integer.MAX_VALUE, A very large number of threads may be created , even to the extent that OOM.
Use ThreadPoolExecutor Constructor to create a thread pool
《 Alibaba Java Development Manual 》 Forced thread pool in is not allowed to use Executors To create , But through ThreadPoolExecutor The way , This processing method makes the students who write the program more clear about the running rules of the thread pool , Avoid the risk of resource depletion
We can create three types of ThreadPoolExecutor:
- FixedThreadPool : This method returns a thread pool with a fixed number of threads . The number of threads in this thread pool is always the same . When a new task is submitted , If there are idle threads in the thread pool , Immediately . If there is no , The new task will be stored in a task queue , When a thread is free , Then I will process the tasks in the task queue .
- SingleThreadExecutor: Method returns a thread pool with only one thread . If more than one task is submitted to the thread pool , The task will be saved in a task queue , Wait for the thread to be idle , Perform tasks in the queue in first in first out order .
- CachedThreadPool: This method returns a thread pool that can adjust the number of threads according to the actual situation . The number of threads in the thread pool is uncertain , But if there are idle threads to reuse , Then the reusable threads will be preferred . If all threads are working , There are new tasks to submit , A new thread processing task will be created . After all threads have finished executing the current task , Will return to the thread pool for reuse .
ThreadPoolExecutor Constructor important parameter analysis
ThreadPoolExecutor 3 The most important parameter
corePoolSize: Number of core threads , Defines the minimum number of threads that can run simultaneously .maximumPoolSize: The maximum number of worker threads allowed in the thread pool .workQueue: The length of the blocking queue . When a new task comes, it will first determine whether the current number of running threads reaches the number of core threads , If it does , The task will be stored in the blocking queue .
ThreadPoolExecutor Other common parameters
keepAliveTime: The number of threads in the thread pool is greater thancorePoolSizeWhen , If no new tasks are submitted at this time , Threads outside the core thread are not immediately destroyed , They wait , Until the wait is overkeepAliveTimeWill be recycled and destroyed ;unit:keepAliveTime Time unit of parameter .threadFactory: Create a thread factory for new threadshandler: When the work queue is full and the number of threads running simultaneously reaches the maximum number of worker threads , The newly added task will adopt the rejection strategy
Use ThreadPoolExecutor
// Creating a thread pool
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAXIMUM_POOL_SIZE,
KEEP_ALIVE,
TimeUnit.SECONDS,
sPoolWorkQueue,
sThreadFactory);
// Submit tasks to the thread pool
threadPool.execute(new Runnable() {
@Override
public void run() {
... // Tasks performed by threads
}
});
// Close thread pool
threadPool.shutdown(); // Set the state of the thread pool to SHUTDOWN, Then interrupt all threads that are not executing tasks
threadPool.shutdownNow(); // Set the state of the thread pool to STOP, Then try to stop all the threads that are executing or suspending tasks , And return to the list of tasks waiting to be performed
The operation process of thread pool

- There are no threads in the thread pool at first , When a task is submitted to the thread pool , The thread pool creates a new thread to perform the task .
- When the number of threads reaches corePoolSize No threads are idle , Then join the task , New tasks will be added workQueue Queue team , Until there are idle threads .
- If the blocking queue selects the bounded queue , Then the task exceeds the block queue size , Will create maximumPoolSize - corePoolSize Number of threads to save .
- If the thread arrives maximumPoolSize If there are still new tasks, the reject policy will be executed .
- When the peak is over , exceed corePoolSize If there is no task to do for a period of time , Need to end saving resources , This time is by keepAliveTime and unit To control .
States in the thread pool
RUNNING: This is the most normal state , Take on new tasks , Processing tasks in waiting queues .
SHUTDOWN: No new task submissions will be accepted , But it will continue to process tasks in the waiting queue .
STOP: No new task submissions will be accepted , Tasks in the queue are no longer waiting to be processed , Interrupt the thread that is executing the task .
TIDYING: All missions destroyed ,workCount by 0, The state of the thread pool is being converted to TIDYING In the state of , Can execute hook method terminated().
TERMINATED:terminated() After the method is over , The state of the thread pool becomes this .

ThreadPoolExecutor Use int The height of 3 Bit to represent thread pool status , low 29 Bits indicate the number of threads
| Status name | high 3 position | Receive new tasks | Processing blocking queue tasks | explain |
|---|---|---|---|---|
| RUNNING | 111 | Y | Y | |
| SHUTDOWN | 000 | N | Y | No new missions , However, the remaining blocking queue will be processed Mission |
| STOP | 001 | N | N | It interrupts the task in progress , And abandon the blocking queue Mission |
| TIDYING | 010 | All the tasks have been completed , The active thread is 0 About to enter End | ||
| TERMINATED | 011 | Put an end to the state |
Compare... Numerically ,TERMINATED > TIDYING > STOP > SHUTDOWN > RUNNING
This information is stored in an atomic variable ctl in , The purpose is to combine the thread pool state with the number of threads , So you can use it once cas Atomic manipulation Assign a value
// c Is the old value , ctlOf The returned result is a new value
ctl.compareAndSet(c, ctlOf(targetState, workerCountOf(c))));
// rs For the high 3 Bits represent the thread pool state , wc For low 29 Bits represent the number of threads ,ctl Is to merge them
private static int ctlOf(int rs, int wc) { return rs | wc; }

ctl Is an atomic integer that packages two concept fields .
1)workerCount: Indicates the valid number of threads ;
2)runState: Indicates the running state of the thread pool , Yes RUNNING、SHUTDOWN、STOP、TIDYING、TERMINATED Equal state
ThreadPoolExecutor Refusal strategy
ThreadPoolExecutor Reject policy definition :
If the work queue is full and the number of threads running simultaneously reaches the maximum number of worker threads ,ThreadPoolTaskExecutor Define some strategies :
ThreadPoolExecutor.AbortPolicy( Default ): Throw outRejectedExecutionExceptionTo refuse to deal with new tasks .ThreadPoolExecutor.CallerRunsPolicy: Use the thread of the caller to execute the task . But this strategy will slow down the submission of new tasks , Affect the overall performance of the program .ThreadPoolExecutor.DiscardPolicy: Don't take on new tasks , Just throw it away .ThreadPoolExecutor.DiscardOldestPolicy: Discard the oldest unprocessed tasks .

for instance :Spring adopt ThreadPoolTaskExecutor Or we can go straight through ThreadPoolExecutor When creating a thread pool , When we don't specify RejectedExecutionHandler Reject the strategy , When configuring the thread pool, the default is ThreadPoolExecutor.AbortPolicy. By default ,ThreadPoolExecutor Will throw out RejectedExecutionException To refuse the new mission , This means that you will lose processing of this task . For scalable applications , It is recommended to use ThreadPoolExecutor.CallerRunsPolicy.
How to terminate a thread pool ?
shutdown:“ gentle ” Close the thread pool . No new assignments , However, the previously submitted tasks will be processed before closing .
shutdownNow:“ Brutal ” Close the thread pool , That is, close the thread pool directly , adopt Thread#interrupt() Method to terminate all threads , It will not wait for the previously submitted task to complete . But it will return the unprocessed tasks in the queue .
In the thread pool submit() and execute() What's the difference ?
Receiving parameters :execute() It can only be carried out Runnable Type of task .submit() It can be executed Runnable and Callable Type of task
Return value :submit() Method can get the result of asynchronous calculation Future object , and execute() No,
exception handling :submit() convenient Exception Handle
submit() Examples of use
public class ExecutorsTest {
private static final String SUCCESS = "success";
private static final int CORE_POOL_SIZE = 5;
private static final int MAX_POOL_SIZE = 10;
private static final int QUEUE_CAPACITY = 100;
private static final Long KEEP_ALIVE_TIME = 1L;
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(
CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(QUEUE_CAPACITY),
new ThreadPoolExecutor.CallerRunsPolicy());
System.out.println("------------------ The task begins ---------------------");
Future<String> future = executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(5000);
System.out.println("submit Method execution task completion " + " thread name: " + Thread.currentThread().getName());
return SUCCESS;
}
});
try {
String s = future.get();
if (SUCCESS.equals(s)) {
String name = Thread.currentThread().getName();
System.out.println(" After return value comparison ,submit Method successfully executed the task thread name: " + name);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("-------------------main thread end---------------------");
}
}
output
------------------ The task begins ---------------------
submit Method execution task completion thread name: pool-1-thread-1
After return value comparison ,submit Method successfully executed the task thread name: main
-------------------main thread end---------------------
submit() return FutureTask object , Through this FutureTask Object call get() Can return submit() Method to pass in a generic class parameter result object , If it is Callable Directly through call() return . This return value can be used to verify whether the task is successfully executed .
The main thread will always block , Wait for the tasks in the thread pool to finish executing , After executing the following statement .
边栏推荐
- 并发工具类
- 1269. number of options left in place
- 22年五月毕设
- 【LeetCode】-- 17.电话号码的字母组合
- Leetcode-647.Palindromic Substrings
- Leetcode hot topic 100 topic 6-10 solution
- Luogu p1091 chorus formation (longest ascending subsequence)
- 常用问题排查工具和分析神器,值得收藏
- Do you use typescript or anyscript
- [deploy private warehouse based on harbor] 3 deploy harbor
猜你喜欢

Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
![[并发进阶]——线程池总结](/img/69/dc8146dafc30f8a8efa012b67aa05c.png)
[并发进阶]——线程池总结

ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
![Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])](/img/1c/4013479ce1fc5b0ff2ebeb754f05a9.png)
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])

Leetcode hot topic 100 topic 21-25 solution

网狐游戏服务器房间配置向导服务定制功能页实现

Zabbix 监控主机是否在线

常用问题排查工具和分析神器,值得收藏

Leetcode hot topic 100 topic 6-10 solution

During unity panoramic roaming, AWSD is used to control lens movement, EQ is used to control lens lifting, and the right mouse button is used to control lens rotation.
随机推荐
Xunwei dry goods | Ruixin micro rk3568 development board TFTP & NFS writing (Part 1)
1266_FreeRTOS调度器启动代码实现分析
Biweekly investment and financial report: capital rush yuan universe game
教育专家王中泽老师:家庭教育重在自己成长
The difference between TCP and UDP
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Leetcode hot topic 100 topic 21-25 solution
Promises/a+ standard Chinese Translation
Group arrays by a specified size
1442. number of triples forming two exclusive or equal arrays
[matlab WSN communication] a_ Star improved leach multi hop transmission protocol [including source code phase 487]
ESP32学习笔记(49)——ESP-WIFI-MESH接口使用
Leetcode-9.Palindrome Numbber
生物序列智能分析平台blog(1)
22年五月毕设
The realization of online Fox game server room configuration battle engagement customization function
Error occurred in pycharm DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Heartless sword Chinese English bilingual poem 001 Love
[deploy private warehouse based on harbor] 2 machine preparation
Starting from scratch (I)