当前位置:网站首页>[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
边栏推荐
- Obtaining server and client information
- 知识、创新、回报。
- 【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
- 剑指offer(一)
- Embedded system driver primary [2] - _ parameters and dependencies under the kernel module
- 【第四章】详解Feign的实现原理
- SQLite数据库连接字符串
- 【面试:并发篇37:多线程:线程池】自定义线程池
- 解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
- Difficulty comparison between high concurrency and multithreading (easy to confuse)
猜你喜欢

科普 | “大姨太”ETH 和 “小姨太”ETC的爱恨情仇

把 VS Code 当游戏机

双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分

Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained

Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition

Linked list implementation and task scheduling

进程调度的基本过程

Leetcode952. Calculate maximum component size by common factor

Web浏览器工作流程解析

Titanic 预测问题
随机推荐
2022.07.13_Daily Question
2022.7.29 Array
CNN--各层的介绍
2022.07.12_每日一题
Conditional statements of shell (test, if, case)
Titanic 预测问题
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
【Go语言入门】一文搞懂Go语言的最新依赖管理:go mod的使用
2022.07.18 _ a day
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
熟悉而陌生的新朋友——IAsyncDisposable
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
2022.07.12_每日一题
LeetCode brush # 376 # Medium - swing sequence
gstreamer's caps event and new_segment event
mysql的建表语句_三种常用的MySQL建表语句
navicat 新建数据库
【微服务】Nacos集群搭建以及加载文件配置
04-SDRAM: Read Operation (Burst)