当前位置:网站首页>面试题:实现死锁
面试题:实现死锁
2022-07-31 22:56:00 【玉面大蛟龙】
实现死锁的思路很简单:
1、首先要有两个资源1和2,有两个线程A和B。
2、线程A抢到了资源1,线程B抢到了资源2。
3、同时,线程A想要资源2,他要等待线程B放弃手里的资源2;线程B想要资源1,他要等待线程A放弃手里的资源1,这个时候就形成死锁了。
public class Main {
public static void main(String[] args) {
//两个资源
Object a = new Object();
Object b = new Object();
//一个线程
new Thread(() -> {
//先去拿a
synchronized (a){
System.out.println(Thread.currentThread().getName() + ": i got a!");
//睡两秒,保证另一个线程能抢到另一个资源
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//再去拿b
synchronized (b){
System.out.println(Thread.currentThread().getName() + ": i got b!");
}
}
}).start();
//另一个线程
new Thread(()->{
//先去拿b
synchronized (b){
System.out.println(Thread.currentThread().getName() + ": i got b!");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//再去拿a
synchronized (a){
System.out.println(Thread.currentThread().getName() + ": i got a!");
}
}
}).start();
}
}边栏推荐
- VOT2021比赛简介
- Recognize anomalies (you will understand after reading this)
- How to debug TestCafe
- [Code Hoof Set Novice Village 600 Questions] Leading to the combination of formulas and programs
- iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
- Go mode tidy reports an error go warning “all” matched no packages
- GateWay implements load balancing
- 周总结
- 一文带你了解 Grafana 最新开源项目 Mimir 的前世今生
- (26) About menu of the top menu of Blender source code analysis
猜你喜欢

A high-quality WordPress download site template theme developed abroad
Dry goods | 10 tips for MySQL add, delete, change query performance optimization

IJCAI2022 | 代数和逻辑约束的混合概率推理

Shell common script: Nexus batch upload local warehouse script

面试突击69:TCP 可靠吗?为什么?

iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools

《ArchSummit:时代的呐喊,技术人听得到》

Collation of knowledge points in Ningbo University NBU IT project management final exam

Binary tree non-recursive traversal

ECCV 2022 Huake & ETH propose OSFormer, the first one-stage Transformer framework for camouflaging instance segmentation!The code is open source!...
随机推荐
[QNX Hypervisor 2.2用户手册]9.16 system
The difference between adding or not adding the ref keyword when a variable of reference type is used as a parameter in a method call in C#
UOS统信系统 - WindTerm使用
Unity-LineRenderer显示一条线
LeetCode 第 304 场周赛
How to debug TestCafe
In Golang go-redis cluster mode, new connections are constantly created, and the problem of decreased efficiency is solved
消息队列存储消息数据的MySQL表格
How to get useragent
Quick Start Tutorial for flyway
[QNX Hypervisor 2.2用户手册]9.15 suppress
SQL注入 Less42(POST型堆叠注入)
Binary tree non-recursive traversal
Dry goods | 10 tips for MySQL add, delete, change query performance optimization
@JsonFormat(pattern="yyyy-MM-dd") time difference problem
iNeuOS industrial Internet operating system, equipment operation and maintenance business and "low-code" form development tools
消息队列消息存储设计(架构实战营 模块八作业)
基于simulink的Active anti-islanding-AFD主动反孤岛模型仿真
网易云信圈组上线实时互动频道,「破冰」弱关系社交
Handwritten a simple web server (B/S architecture)