当前位置:网站首页>[Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
[Interview: Concurrency 38: Multithreading: Thread Pool] Basic concepts of the ThreadPoolExecutor class
2022-07-31 07:46:00 【I cream】
[Interview: Concurrency 38: Multithreading: Thread Pool] Basic Concepts of ThreadPoolExecutor Class
00. Foreword
If you have any questions please point them out, thanks.
01. Introduction

The ThreadPoolExecutor class implements the ExecutorService interface. The difference between it and the ScheduledThreadPoolExecutor class is that the ScheduledThreadPoolExecutor isThe thread pool with task scheduling function implements this and we will talk about it later.
02.ThreadPoolExecutor-Pool Status
ThreadPoolExecutor uses the upper 3 bits of int to represent the thread pool status, and the 29th bit represents the number of threads
| State Name | High 3 | Receive new assignments | Handling blocking queue tasks | Description |
|---|---|---|---|---|
| RUNNING | 111 | Y | Y | |
| SHUTDOWN | 000 | N | Y | Will not receive new tasks, but will process the remaining tasks in the blocking queue |
| STOP | 001 | will interrupt the running task and discard the blocking queue task | ||
| TIDYING | 010 | The task is fully executed, and the active thread is 0 and is about to end | ||
| TERMINAIED | 011 | End state | ||
| Why the thread pool status and the number of thread pools are represented by one number instead of two, the reason is that these two information are stored in an atomic variable ctl, so that cas can be used only once in the futureAtomic operation for assignment. |
03. Constructor

We can see that the constructor of the ThreadPoolExecutor class has many parameters, one by oneExplanation
corePoolSize: number of core threads
maximumPoolSize: maximum number of threads
keepAliveTime: time to live - for rescue threads
br>unit: time unit - for emergency threads
workQueue: blocking queue
threaFactory: thread factory - can be created for threads
hander: Rejection Policy
The difference between ThreadPoolExecutor and our custom thread pool
- Help us customize the thread pool. There is only one thread in the thread pool, which is the core thread, and our core thread has the take method and the pull method.But the ThreadPoolExecutor thread pool has two types of threads: one is corePoolSize when the number of core threads, and the other is when the number of emergency threads is maximumPoolSize-corePoolSize, and the core thread has only the take method and the emergency thread has only the pull method, which is why keepAlivTime and unit are only for emergency threads..
- The name of the thread in the thread pool cannot be specified in the custom thread pool, and the thread name in the ThreadPoolExecutor thread pool can be changed through the threadFactory parameter
- Custom thread pool and ThreadPoolExecutor thread pool rejection policy
How it works
Introduction
We know that there are only core threads in the custom thread pool, so the process of the custom thread pool is:
Create a task -> give it to an idle core thread for execution -> exceed the part of the taskPut into the task queue -> if there are idle core threads at this time, get the task execution from the task queue -> if there are no idle threads and the task queue is full -> the production thread executes the rejection policy
ThreadPoolExecutor thread poolThere are core threads and emergency threads, so the process of the ThreadPoolExecutor thread pool is:
Additional: The core thread of the ThreadPoolExecutor thread pool has no timeout period, but the emergency thread does.
Create a task -> give it to an idle core thread for execution -> put the excess task into the task queue -> if there is an idle core thread at this time -> if there is an idle core thread at this time, get the task execution assignment from the task queueFor the core -> if there are no idle threads and the task queue is full -> the extra tasks created by the emergency thread are handed over to the emergency thread for execution -> if the emergency thread is no longer idle -> the production thread executes the rejection policy
Explanation
It can be seen that the role of the emergency thread is to create an emergency thread when the core thread and the task queue are full. If the emergency thread does not obtain a task within the timeout period, it will be removed from the thread pool.
Deny Policy
If the core thread is not idle, the task queue is full, and the emergency thread is not idle, the rejection policy is executed in this case. The rejection policy jdk provides four implementations.
- AbortPolicy makes the caller throw a RejectedExecutionException, which is the default policy
- CallerRunsPolicy lets callers run tasks
- DiscardPolicy abandons this task
- DiscardOldestPolicy Discard the oldest task in the queue and replace it with this task
边栏推荐
- 【科普向】5G核心网架构和关键技术
- 电压源的电路分析知识分享
- 超级详细的mysql数据库安装指南
- 深度学习通信领域相关经典论文、数据集整理分享
- 2022.07.12_每日一题
- Obtaining server and client information
- Environment_Variable_and_SetUID
- Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
- Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
- 解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
猜你喜欢

【Go】Go 语言切片(Slice)

【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止

2022.07.13_每日一题

基金投顾业务

基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例

Leetcode952. 按公因数计算最大组件大小

'vite' is not an internal or external command, nor is it a runnable program or batch file.

The Perfect Guide|How to use ODBC for Agentless Oracle Database Monitoring?

【微服务】Nacos集群搭建以及加载文件配置

PCB抄板
随机推荐
codec2 BlockPool:不可读库
2022.07.12_每日一题
Jetpack Compose学习(8)——State及remeber
2022.07.15_每日一题
ros小乌龟画图
How to set the computer password?How to add "safety lock" to your computer
单点登录 思维导图
Conditional statements of shell (test, if, case)
基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例
【微服务】Nacos集群搭建以及加载文件配置
Titanic 预测问题
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
Log4net 思维导图
测试 思维导图
【微服务】 微服务学习笔记二:Eureka注册中心的介绍及搭建
MySQL安装到最后一步 write configuration file 失败 怎么办?及后安装步骤
LeetCode brush # 376 # Medium - swing sequence
DAY18:XSS 漏洞
Linked list implementation and task scheduling
2022.07.12 _ a day