为什么这段代码在jdk12中没有引发死锁呢?

包com;
公共类DeadLockDemo {    私有静态字符串A =“ A”;    私有静态字符串B =“ B”;    公共静态void main(String [] args)抛出InterruptedException {        新的DeadLockDemo()。deadLock();    }    私人无效deadLock(){        线程t1 =新线程(新Runnable(){@Overridepublic void run(){                已同步(A){                    尝试 {                        Thread.currentThread()。sleep(2000);                    } catch(InterruptedException e){                        e.printStackTrace();                    }                }                已同步(B){                    System.out.println(“ 1”);                }            }        });        线程t2 =新线程(新Runnable(){@Overridepublic void run(){                已同步(B){                    已同步(A){                        System.out.println(“ 2”);                    }                }            }        });        t1.start();        t2.start();    }}


#学习路径#
全部评论
还记得哲学家就餐问题是什么样的吗? 定义可是:一个人拿到2个东西,处理完了,才放手 你现在t1里,拿了A,然后放手,再拿B,那当然不会死锁了。 会是: 1. t1 拿 A, t2 拿 B 等 A 2. t1 放 A, t2马上拿A(持有AB)开始处理,t1等B 3. t2 放 AB, t1 拿A再放A
点赞 回复 分享
发布于 2021-03-20 22:31

相关推荐

明天不下雨了:我靠2022了都去字节了还什么读研我教你****:你好,本人985电子科大在读研一,本科西南大学(211)我在字节跳动实习过。对您的岗位很感兴趣,希望获得一次投递机会。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务