当前位置:网站首页>Interview Question: Implementing Deadlocks
Interview Question: Implementing Deadlocks
2022-07-31 23:32:00 【Jade-faced Jiaolong】
The idea of implementing deadlock is very simple:
1. First, there must be two resources 1 and 2, and two threads A and B.
2. Thread A grabs resource 1, and thread B grabs resource 2.
3. At the same time, if thread A wants resource 2, he has to wait for thread B to give up resource 2 in his hand; if thread B wants resource 1, he has to wait for thread A to give up resource 1 in his hand.locked.
public class Main {public static void main(String[] args) {//two resourcesObject a = new Object();Object b = new Object();// a threadnew Thread(() -> {//go to get a firstsynchronized (a){System.out.println(Thread.currentThread().getName() + ": i got a!");//Sleep for two seconds to ensure that another thread can grab another resourcetry {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}// go to get bsynchronized(b){System.out.println(Thread.currentThread().getName() + ": i got b!");}}}).start();// another threadnew Thread(()->{//go to get b firstsynchronized(b){System.out.println(Thread.currentThread().getName() + ": i got b!");try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}// go to get asynchronized (a){System.out.println(Thread.currentThread().getName() + ": i got a!");}}}).start();}}边栏推荐
- lua入门案例实战123DIY
- 《ArchSummit:时代的呐喊,技术人听得到》
- 如何导入 Golang 外部包并使用它?
- vim的基本使用概念
- cobaltstrike
- The latest masterpiece!Alibaba just released the interview reference guide (Taishan version), I just brushed it for 29 days
- Mysql environment installation under Linux (centos)
- TypeScript 的组件
- 【Acwing】The 62nd Weekly Game Solution
- 面试突击69:TCP 可靠吗?为什么?
猜你喜欢

新产品如何进行网络推广?
不知道该怎么办的同步问题

Unity - LineRenderer show a line

C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处

Pytest初体验

MLP神经网络,GRNN神经网络,SVM神经网络以及深度学习神经网络对比识别人体健康非健康数据

UOS - WindTerm use

2022-07-31:给出一个有n个点,m条有向边的图, 你可以施展魔法,把有向边,变成无向边, 比如A到B的有向边,权重为7。施展魔法之后,A和B通过该边到达彼此的代价都是7。 求,允许施展一次魔法

Network security - crack WiFi through handshake packets (detailed tutorial)
Mysql environment installation under Linux (centos)
随机推荐
《ArchSummit:时代的呐喊,技术人听得到》
Daily--Kali opens SSH (detailed tutorial)
Learn about C# anonymous methods
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#
C#中引用类型的变量做为参数在方法调用时加不加 ref 关键字的不同之处
SVN服务器搭建+SVN客户端+TeamCity集成环境搭建+VS2019开发
Weekly Summary
@JsonFormat(pattern="yyyy-MM-dd") time difference problem
[QNX Hypervisor 2.2用户手册]9.14 set
EntityFramework保存到SQLServer 小数精度丢失
lua入门案例实战1234定义函数与标准函数库功能
内核对设备树的处理
Pytest first experience
Unity-通过预制件和克隆方法动态实现各个UGUI下控件的创建和显示
信奥学习规划 信息学竞赛之路(2022.07.31)
【Acwing】The 62nd Weekly Game Solution
Interview assault 69: TCP reliable?Why is that?
Thinking and Implementation of Object Cache Service
/etc/sysconfig/network-scripts configure the network card
The difference between /usr/local/bin and /usr/bin