解决线程死锁的方案2:

背景: 造成线程死锁的条件中,有一个就是多个线程对多个资源访问的时候没有按照顺序访问造成的。这边博文讲解解决方案的原理。
多个线程对多个资源访问时,我们可以引入Lock机制,利用Lock的tryLock和Lock.unLock方法就可以解决死锁的问题。

  1. 示例代码:

     public class TryLock {
    
         private static Lock obj1 = new ReentrantLock(); // 代表对象1
         private static Lock obj2 = new ReentrantLock(); // 代表对象2
     
     
         private static void methodForMainThread() {
             String threadName = Thread.currentThread().getName();
             Random random = new Random();
             while(true) {
                 if (obj2.tryLock()) {
                     System.out.println(threadName + ": get obj2");
                     try{
                         if (obj1.tryLock()) {
                             try{
                                 System.out.println(threadName + ": get obj1");
                                 break;
                             }finally {
                                 obj1.unlock();
                             }
                         }
                     }finally {
                         obj2.unlock();
                     }
                 }
                 try {
                     Thread.sleep(random.nextInt(3));
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
         }
     
         private static void methodForChildThread() {
             String threadName = Thread.currentThread().getName();
             Random random = new Random();
             while(true) {
                 if (obj1.tryLock()) {
                     System.out.println(threadName + ": get obj1");
                     try{
                         if (obj2.tryLock()) {
                             try{
                                 System.out.println(threadName + ": get obj2");
                                 break;
                             }finally {
                                 obj2.unlock();
                             }
                         }
                     }finally {
                         obj1.unlock();
                     }
                 }
                 try {
                     Thread.sleep(random.nextInt(3));
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
         }
     
     
         static class FruitThread extends Thread {
     
             @Override
             public void run() {
                 super.run();
                 methodForChildThread();
     
             }
         }
         public static void main(String[] args) {
             Thread.currentThread().setName("主线程");
             FruitThread fruitThread = new FruitThread();
             fruitThread.start();
             methodForMainThread();
         }
    }
    
  2. 运行结果:
    在这里插入图片描述

总结: 当遇到多个线程对多个资源访问,而且没有按照顺序访问时,这个时候引入Lock机制也是可以解决死锁的问题的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不搞复杂看不懂的文章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值