zoukankan      html  css  js  c++  java
  • 12_死锁问题

    【死锁例子】

    public class Demo {
        public static void main(String[] args) {
            DeadLockThread dt1=new DeadLockThread(true);
            DeadLockThread dt2=new DeadLockThread(false);
            
            Thread t1=new Thread(dt1,"筷子线程");
            Thread t2=new Thread(dt2,"刀叉线程");
            
            t1.start();
            t2.start();
        }
    }
    
    class DeadLockThread implements Runnable{
    
        static Object chopsticks=new Object();  //筷子
        static Object knifeFork=new Object();   //刀叉
        private boolean flag;
        
        public DeadLockThread(boolean flag){
            this.flag=flag;
        }
        
        @Override
        public void run() {
            if(flag){   //如果为true,默认为筷子线程
                while(true){
                    synchronized (chopsticks) {  
                        System.out.println(Thread.currentThread().getName()+"---if---chopsticks");
                        synchronized (knifeFork) {   //等待刀叉线程的锁释放
                            System.out.println(Thread.currentThread().getName()+"---if---knifefork");
                        }
                    }
                }
            }else{    //如果为false,默认为刀叉线程
                while(true){
                    synchronized (knifeFork) {
                        System.out.println(Thread.currentThread().getName()+"---else---knifefork");
                        synchronized (chopsticks) {  //等待筷子线程的锁释放
                            System.out.println(Thread.currentThread().getName()+"---else---chopsticks");
                        }
                    }
                }
            }
        }
    }

    【运行结果】

    【分析】

    创建两个线程"筷子线程"和"刀叉线程",flag值分别为true和false,分别执行run方法中的if和else的代码块,注意chopstickknifefork对象锁都是静态变量,所以两个线程中的static变量时一致的。当筷子线程拥有chopsticks锁,此时需要获得knifefork锁才能继续执行,而刀叉线程拥有knifefork锁,此时需要chopsticks锁才能继续执行,此时就陷入了一个矛盾,两个线程都需要对方占用的锁,但是都无法释放自己的锁,于是两个线程都处于了挂起状态,造成上面的死锁现象。

  • 相关阅读:
    倒下
    我还能相信谁

    工作这点事
    人,这东西
    祝福
    路,公车和鞋子
    那片海
    document.querySelector bug All In One
    js logical or assignment bug All In One
  • 原文地址:https://www.cnblogs.com/HigginCui/p/6136686.html
Copyright © 2011-2022 走看看