当前位置:网站首页>A simple deadlock example
A simple deadlock example
2022-06-13 00:25:00 【Survivors with dreams behind their backs】
Preface :
When interviewing meituan in the front time , The interviewer came up with such a question : Write a deadlock ....
I only remember that resources are mutually exclusive 、 The result of looting , But I haven't written it manually , As a result, it was not written .
Deadlock generation conditions :
- Mutually exclusive ;
The requested resources are mutually exclusive , When used by a thread , Other threads are not available .
- Request and maintain ;
Threads own resources 1, To ask for resources 2. If the resource at this time 2 Can't get , Thread entry “ Request resources 2 And keep resources 1” The state of .
- Loop waiting for
Multiple threads request each other's resources at the same time , At the same time, it also holds the resources needed by the other party , Form a cycle .
- Not to deprive
A thread can only release its own resources , Can't be deprived of .
Deadlock resolution :
Destroy one or more conditions for deadlock generation .
Deadlock example :
package com.demo.test.demo.api;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/** * Deadlock example * * @author zhao.hualuo * Create at 2021/7/13 */
public class DeadLockDemo implements Runnable {
/** Conditions under which resource grabbing occurs */
public int flag = 1;
/** Robbed resources */
static final String STR1 = " resources 1";
static final String STR2 = " resources 2";
/** Thread pool */
public static ExecutorService service = Executors.newFixedThreadPool(3);
public static void main(String[] args) {
// Threads 1
DeadLockDemo demo1 = new DeadLockDemo();
demo1.flag = 1;
service.execute(demo1);
/** Threads 2 */
DeadLockDemo demo2 = new DeadLockDemo();
demo2.flag = 0;
service.execute(demo2);
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ",flag=" + flag);
if(flag == 0) {
// Occupy resources 1, Grab resources 2
synchronized(STR1) {
try {
System.out.println(Thread.currentThread().getName() + " It's locked " + STR1 );
// sleep 3 Second is to wait for resources 2 Be occupied by the other party , Then you can't grab it , It's deadlocked
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + " Get ready to get " + STR2);
synchronized(STR2) {
System.out.println(Thread.currentThread().getName() + " Acquired " + STR2);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
// Occupy resources 2, Grab resources 1
synchronized(STR2) {
try {
System.out.println(Thread.currentThread().getName() + " It's locked " + STR2 );
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + " Get ready to get " + STR1);
synchronized(STR1) {
System.out.println(Thread.currentThread().getName() + " Acquired " + STR1);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Execution results :
Code reference from :《 What is a deadlock ? Write deadlocks yourself . An example of a deadlock . Four conditions of deadlock , How to avoid deadlock .》
边栏推荐
- What occupation is suitable for PMP?
- 2022施工員-設備方向-通用基礎(施工員)操作證考試題及模擬考試
- Pipelines in go concurrency mode
- [CISCN2019 华北赛区 Day2 Web1]Hack World --BUUCTF
- [LeetCode]9. Palindromes thirty-two
- 在 Golang 中构建 CRUD 应用程序
- Is the brokerage account in qiniu business school safe? Is the account opening rate low
- 2022 beautician (technician) certificate title and answer
- vs studio_ How to use scanf in 2022
- How to quickly query the mobile phone number home and operator
猜你喜欢
随机推荐
【Matlab】基础运算
After so long use, CSDN has finally opened a blog
ucore lab2
[hcie discussion] multicast igmp-a
MySql索引
【Matlab】符号计算
新增博客地址
Machining Industry MES system Mold Industry MES system CNCl Medium Industry MES System MES code scanning and reporting MES data collection
[supersocket 2.0] supersocket 2.0 from the beginning to the end
PMP training organization
MySQL index
哲学和文学的区别
Is the newly graduated college student taking BEC or PMP? PM who wants to transfer to another job in the future
2022 constructor - Equipment direction - General Foundation (constructor) operation certificate examination questions and simulation examination
Solution to the problem of closing the watchdog of STM32 in the sleep mode (stop/standby)
[hcie discussion] rr-a
[matlab] matrix operation
Converting Chinese numbers to Arabic numbers in Delphi
Do you have to read for PMP?
3、 Storage system