当前位置:网站首页>[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
边栏推荐
猜你喜欢

完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?

Shellshock

【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念

解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie

2022.07.20_Daily Question

【科普向】5G核心网架构和关键技术

03-SDRAM: Write operation (burst)

tidyverse笔记——dplyr包

LeetCode brush # 376 # Medium - swing sequence
![Embedded system driver primary [2] - _ parameters and dependencies under the kernel module](/img/c2/22c1e087b92ed2c898f97bcf0e818b.png)
Embedded system driver primary [2] - _ parameters and dependencies under the kernel module
随机推荐
《白帽子说Web安全》思维导图
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
DAY18: Xss Range Clearance Manual
客户端navicat安装教程
【微服务】(十六)—— 分布式事务Seata
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
2022.07.12_Daily Question
从 Google 离职,前Go 语言负责人跳槽小公司
MySQL详解
Titanic 预测问题
LeetCode brush # 376 # Medium - swing sequence
bcos简介及自序
opencv、pil和from torchvision.transforms的Resize, Compose, ToTensor, Normalize等差别
完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?
事务的四大特性
2022.07.15_每日一题
【第四章】详解Feign的实现原理
R——避免使用 col=0
测试 思维导图