当前位置:网站首页>Deadlock event of thread pool
Deadlock event of thread pool
2022-07-25 10:31:00 【scwMason】
Everyone pays attention to official account , More high-quality tutorial series are in the centrifuge program :
| background
Here's the thing , A project in hand uses thread pools to handle a large number of asynchronous tasks , The initial goal was to reduce time-consuming for parallel processing , But the traffic the day before yesterday was offline due to other colleagues job It has increased dozens of times , Let's not discuss this normative problem first . Due to flow spikes , The service timed out , Cause upstream services to fail to get results , The timeout configuration of the front end for their server is unreasonable , It directly leads to white pages , That sounds ridiculous

, The general problem request process is as follows :

| screening
Timeout occurs but the service CPU Occupation is not high , There is no trigger HPA( Dynamic capacity ), We checked the threads of a machine in the cluster. A large number of threads TimeWait, And locate the problem code , First, let's look at our thread pool definition ( Example )
ThreadPoolExecutor tpe = new ThreadPoolExecutor(coreNum,coreNum*2,100L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<>(),namedThreadFactory);
Questions as follows :
There is no limit to the length of the queue
It is better to add logs or alarms when the rejection strategy goes to
Then the most critical place , There is such a parent-child task relationship in this project :
/**
* Father task , It executes subtasks asynchronously
*/
static class FatherTaskTwo implements Callable<String> {
@Override
public String call() throws Exception {
SonTask sonTask = new SonTask();
Future<String> future = tpe.submit(sonTask);
return future.get();
}
}
/**
* The subtasks
*/
static class SonTask implements Callable<String> {
@Override
public String call() throws Exception {
// Deal with some business logic
return null;
}
}
public static void main(String[] args) throws Exception {
FatherTaskTwo fatherTaskTwo = new FatherTaskTwo();
Future<String> future = tpe.submit(fatherTaskTwo);
// Main thread adds timeout get Blocking
future.get(1000L, TimeUnit.MILLISECONDS);
} Here are a few questions :
1. Father task get The result of the subtask has no timeout
2. Parent and child tasks share a thread pool
What's the problem with this ? Is the key to today : Deadlock
We assume that there are at most four threads processing at the same time , Our normal situation may be like this :

ok, Why is this normal , You can imagine sub tasks 1 And subtasks 2 Will be quickly implemented , So the subtask 3 and 4 Will be executed immediately , But the parent task 3 And parent task 4 Is to wait until the subtask 3 And subtasks 4 The result will be returned after execution , Because father and son are dependent , And in the parent task get Subtask is No timeout set , But the situation is good , Because subtasks 34 In subtask 12 After execution, you can get the thread resources to execute , Then put the parent task 34 Also digest , But if this is the case :

Then there are four parent tasks that cannot be digested , The reason lies in :
The parent task depends on its own child task
The parent task has no timeout , It will keep blocking , Conditions that cannot break the deadlock
This creates a deadlock , The thread is suspended due to blocking, so CPU It will not rise due to the problem of thread pool
| Conclusion
Although the whole event link has many unreasonable places , For example, degradation between services 、Online/Offline And so on , But we mainly focus on the internal problems of the application , It is mainly the deadlock caused by the wrong use of thread pool , Then there are roughly several solutions from different angles :
Thread pool isolation , Dependent threads process in different pools
Be sure to set get Timeout time
There is no need to dismantle so many levels of tasks , Try to be as flat as possible
边栏推荐
- 4. Children next door will know all kinds of shell symbols {}[], etc
- Ansible部署指南
- 将 conda 虚拟环境 env 加入 jupyter kernel
- Software test notes, test case design
- Angr(一)——安装
- Angr(六)——angr_ctf
- Reproduce asvspoof 2021 baseline rawnet2
- Erlang (offline deployment)
- 【无标题】
- Supervisor deployment (offline deployment requires downloading the deployment package in advance)
猜你喜欢
随机推荐
Usage of string slicing
Multithreading -- callable interface, lambda
conda 配置深度学习环境 pytorch transformers
Yiwen society, three necessary packet capturing tools for hackers
Test plan and test plan
将 conda 虚拟环境 env 加入 jupyter kernel
21. Merge Two Sorted Lists
11. Iptables firewall
Pytorch calculates the loss for each sample in the batch
Dynamic planning, shopping list problem
For cycle: daffodil case
8.shell文件处理三剑客之sed
Angr (IV) -- angr_ ctf
Angr (III) - angr_ ctf
Simple addition calculator
安装mysql时,string the service 安装失败>mysql80启动失败
4.隔壁小孩都会的,各种shell符号{}[]等
Angr (V) - angr_ ctf
Angr(九)——angr_ctf
Supervisor deployment (offline deployment requires downloading the deployment package in advance)







