美文网首页
Java并发案例01---多线程之死锁

Java并发案例01---多线程之死锁

作者: 枫叶_huazhe | 来源:发表于2017-11-07 17:37 被阅读0次

    多线程之死锁案例一

    package example;
    /**
     * 模拟死锁
     * @author maple
     *
     */
    public class DeadLock {
        
        public int flag = 1;
        /**
         * 这里必须要用静态的锁对象,o1,o2
         * 因为new两个对象时,他们共享静态变量,持有的锁才会有冲突
         */
        private static Object o1 = new Object();
        private static Object o2 = new Object();
    
        public DeadLock(int flag) {
            this.flag=flag;
        }
    
        
        public void m(){
            if(flag == 1){
                synchronized (o1) {
                    System.out.println("i am in  flag 1");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    
                    synchronized (o2) {
                        System.out.println("i have o2");
                    }
                    
                }
            }
            
            if(flag == 0){
                synchronized (o2) {
                    System.out.println("i am in  flag 0");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    
                    synchronized (o1) {
                        System.out.println("i have o1");
                    }
                    
                }
            }
            
            
        }
        
        public static void main(String[] args) {
            DeadLock d1 = new DeadLock(1);
            DeadLock d2 = new DeadLock(0);
            
            new Thread(()->d1.m(),"t1").start();
            new Thread(()->d2.m(),"t2").start();
        }
        
    }
    
    

    相关文章

      网友评论

          本文标题:Java并发案例01---多线程之死锁

          本文链接:https://www.haomeiwen.com/subject/pxwjmxtx.html