当前位置:网站首页>Multithreading and high concurrency V: detailed explanation of wait queue, executor and thread pool (key)
Multithreading and high concurrency V: detailed explanation of wait queue, executor and thread pool (key)
2022-06-28 04:15:00 【smartjiang-java】
List of articles
1: Waiting in line
1.1: Queue introduction
- The benefits of creating a thread pool are not covered here , Direct delivery of dry goods . First , To understand thread pools , First, we need to know some queues , This is the foundation of thread pool .
- Multithreaded container , Think more about it later Queue, Think less about List,Set.
- Here is an interview question :Queue Comparison list A better place ? It has a multi thread friendly interface , There are the following ways :
offer:---- amount to add, There will be a return value , Successfully returned ture
poll: Take the data and remove fall
peek: Data retrieval is not remove fall - BlockingQueue There are frequent queues in the thread pool : It is a blocking queue . The following blocking methods are added .
put(), Put in , Full will block
take(), Take it out , Empty will block .1.2: Classification of queues
BlocKingQueue There are the following :
A: LinkedBlockingQueue
Linked list implementation , The maximum waiting queue length is Integer.Max_Value**
B:ArrayBlockingQueue
You can set the initial queue size **
C:DelayQueue
Sort by waiting time , Adjust tasks according to time degree , Essentially, PriorityQueue . The implementation class should implement Delay Interface , Incoming wait time , How often does it run
D: PriorityQueue
Inherit AbstractQueue, The model of binary tree , Sort the added elements , The default is ascending
E: SynchronizedQueue
Capacity of 0, Don't throw anything in it add, You can only install when you are waiting for something in front put, Pass things **
F:TransferQueue
There are transfer Method , Finished loading , Blocking , Waiting for something to be taken away , Just left
1.2: Common rejection strategies
A:new ThreadPoolExecutor.AbortPolicy()
AbortPolicy Always throw exceptions , No special usage scenarios , The default is the reject policy . For some important businesses , You can use this option No strategy , It is convenient to find the cause of the error in time
B:new ThreadPoolExecutor.CallerRunsPolicy()
CallerRunsPolicy Throw the task to the thread that starts the thread pool to execute . Applicable to less important business scenarios , Don't throw mistakes , For example, the amount of blog reading
C:new ThreadPoolExecutor.DiscardOldestPolicy()
DiscardOldestPolicy Let the first person who enters the blocking queue leave , Then go in and line up . Discard the earliest ones that enter the blocking queue , The typical love the new and hate the old , See if you need for the old task .
D:new ThreadPoolExecutor.DiscardPolicy()
DiscardPolicy Discard tasks directly . Throw the task to the thread of the thread pool itself to run , Generally, failure is not allowed 、 The performance requirements are not high 、 It can be used in the scenario of less concurrency . otherwise , Easy to degrade performance
2:Executor
2.1:Runnable Pick up ⼝ and Callable Interface
- We know , There is a task to start in the thread , Let the thread work . commonly There are two ways to implement a task : Realization Runnable Pick up ⼝ and Callable Interface .
An interview question : Realization Runnable Interface and Callable Interface differences ?
Runnable Interface in Java 1.0 There has always been , but Callable Only in Java 1.5 Introduction in , The purpose is to deal with Runnable There is no return value or a check exception is thrown . This return value is usually used as Future Object to receive ,Future.get(); Blocking method , It will not return until there is a result , Stop blocking
2.2:FutureTask
- except Future, also FutureTask: amount to Future+Runnable, It is a task , Another Future, You can also receive the executed results . meanwhile , Another Runnable, Sure new A thread or thread pool to execute .
2.3 About Executor Interface :
. 1:Executor Interface : practitioners , The definition and operation of a task can be separated . There is only one way executr(), Let the thread execute .
2:ExecutorService Interface : Inherit Executor Interface , Improve the life cycle of task executor . also submit(Callable perhaps Runnable) Method , Submit tasks asynchronously , The return value is one Future
3:CompletableFuture: Run a task , There will be a return value , The type is double, I can accept .allOf(): Manage a bunch of tasks , When all the tasks are finished , Will continue to carry on . Manage multiple Future Result
3: Thread pool (ThreadPoolExecutor )
3.1: Introduce
ThreadPoolExecutor Inherited from ExecutorService Inherit Executor, The thread pool maintains two collections : One is a collection of threads (HashSet), One is a collection of tasks
3.2. Custom thread pool : Seven parameters ( Interview focus )
// Custom thread , But it's not specific enough , No custom reject policy
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 4,
60, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(4),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy());
Seven parameters :
A:corePoolSize: Number of core threads , The number of threads that initially exist in the thread pool , The core thread lives forever ( You can control whether to close the core thread through parameters , Do not turn off by default )
B:maximumPoolSize: Maximum number of threads
C:keepAliveTime: Thread idle time , Beyond that time , Thread resources are returned to the operating system
D:TimeUnit: The unit of survival time
E:workQueue: The thread queue ,blockIngQueue–> LinkedBlockingQueue,ArrayBlockingQueue etc.
F:ThreadFactory: Thread factory , Create a new thread , You can specify the thread name in your own way , The guardian thread
G:Handler: Refusal strategy ( Saturated strategy ): Thread pool busy , The waiting queue is full , The default is 4 Kind of , You can customize , It's usually custom , You can set the thread name , Prevent tracking when errors occur , In addition, some contents and states of threads can be saved in customization .
3.3: The default implementation of the thread pool
adopt Executors( Thread pool factory ), Four thread pools can be implemented , however 《 Alibaba Java Development Manual 》 Forced thread pool in is not allowed to use Executors To create , Let's analyze why with the source code .
A:SingleThreadPool
Thread pool with only one thread : It can ensure that the thrown tasks are executed in sequence **
// establish
ExecutorService service = Executors.newSingleThreadExecutor();
Here is the source code :
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
// The waiting queue here is LinkedBlockingQueue, Can cause Integer.Max_Value
// The thread is waiting , Pile up a lot of requests , This may result in resource exhaustion , Which leads to OOM
new LinkedBlockingQueue<Runnable>()));
}
B:CachedThreadPool
A pool of threads with an indefinite number of threads
// establish
ExecutorService service = Executors.newCachedThreadPool();
Here is the source code :
public static ExecutorService newCachedThreadPool() {
// // The maximum number of threads is Integer.MAX_VALUE, A large number of threads may be created , Which leads to OOM
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
C:FixedThreadPool
A fixed number of thread pools , Thread parallelism
// establish
ExecutorService service = Executors.newFixedThreadPool(10);
Here is the source code :
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
// The waiting queue here is LinkedBlockingQueue, Can cause Integer.Max_Value
// The thread is waiting , Pile up a lot of requests , This may result in resource exhaustion , Which leads to OOM
new LinkedBlockingQueue<Runnable>());
}
D:ScheduledThreadPool:
Timed task thread pool There are three parameters
Here is the source code :
public ScheduledThreadPoolExecutor(int corePoolSize) {
The maximum number of threads is Integer.MAX_VALUE, A large number of threads may be created , Which leads to OOM
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}
4: Other thread pools
4.1:WorkStealingPool
Each thread in the thread pool has its own task queue , After a thread finishes executing its own task , Will go to other threads to get tasks to run . It's essentially a ForkJoinPool, It just provides a more convenient interface
4.2 :ForkJoinPool
A thread pool that divides large tasks into many small tasks . You can also cut small tasks into smaller tasks . If you need to summarize the tasks , The sub task is summarized to the parent task , The parent task is summarized to the following task . It needs to be defined as a task that can be forked ForkJoinTask, In general use RecursiveTask( Tasks with return values ),RecursiveAction( Tasks with no return value ), Both are inherited from ForkJoinTask**
4.3: Interview questions
That's all for this one , After learning multithreading , An interview question : Suppose an alarm clock service is provided , There are a lot of people subscribing to this service ,10 Hundreds of millions of people , How to optimize ?
Ideas : Distribute subscription tasks to other edge servers , Use thread pools on each server + Service queue
Be careful : This article only represents the personal views of novice bloggers , If something is wrong or there is a better idea through the technology , Welcome to leave a message , Sharing and communication make us progress , thank you .
边栏推荐
- From zero to one, I will teach you to build a "search by text and map" search service (I)
- Understanding and learning of parental delegation mechanism
- RT thread bidirectional linked list (learning notes)
- 云厂商为什么都在冲这个KPI?
- 从零到一,教你搭建「以文搜图」搜索服务(一)
- 11_刻意练习精讲
- 利用ELK 搭建日志分析系统(二)—— 部署安装
- Analyzing the comprehensive application ability of educational robot
- leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
- 领歌leangoo敏捷看板工具新增导出卡片文档和粘贴共享脑图节点功能
猜你喜欢

applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L

美创数据安全管理平台获信通院“数据安全产品能力验证计划”评测证书

美创入选“2022 CCIA中国网络安全竞争力50强”榜单

2021 year end summary and 2022 outlook

多项目开发入门,基础设计 类库项目使用

Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?

Multi project design and development · introduction to class library project

02 mongodb data types, important concepts and common shell instructions

The operating mechanism of spectrogram in audio Science

【小程序实战系列】电商平台源码及功能实现
随机推荐
Several important physical concepts
Understanding and learning of parental delegation mechanism
CDC全量抽取mysql数据时,怎么才能加快ChunkSplitter呢?
利用ELK 搭建日志分析系统(二)—— 部署安装
Building a server monitoring platform with telegraf influxdb grafana
MSC 307(88) (2010 FTPC Code)第2部分烟气和毒性测试
Introduction to SQLSERVER database
La norme européenne en 597 - 1 pour les meubles est - elle la même que les deux normes en 597 - 2 pour les ignifuges?
ELK 搭建日志分析系统 + Zipkin服务链路追踪整合
多项目开发入门,基础设计 类库项目使用
Reverse a stack with recursive functions and stack operations only
From meeting a big guy to becoming a big guy, shengteng AI developer creation day brings infinite possibilities to developers
MSc 307 (88) (2010 FTPC code) Part 2 smoke and toxicity test
AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
The operating mechanism of spectrogram in audio Science
关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
Detailed explanation of KVM common commands
Secouer le son et se battre ~ prêter attention au blogueur
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
03 MongoDB文档的各种增加、更新、删除操作总结