Java死锁例子

产生死锁的四个条件:占有等待、互斥、循环等待、非剥夺。

Java死锁栗子:

public class Main {
    //两把钥匙
    static Object key1 = new Object();
    static Object key2 = new Object();

    public static void method1() {
        System.out.println(Thread.currentThread().getName()+" enter method1");
        synchronized(key1){
            try {
                //线程1获取key1后睡眠一段时间,等候线程2获得key2
                Thread.sleep(300);
                //调用method2(),占有key1状态下申请key2
                method2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("leave method1");
    }

    public static void method2() {
        System.out.println(Thread.currentThread().getName()+" enter method2");
        synchronized(key2){
            try {
                //睡眠时间比线程1稍微长些,目的让线程1申请key2时线程2处于占有key2状态
                Thread.sleep(500);
                //占有key2状态下申请key1
                method1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("leave method2");
    }

    public static void main(String[] args){
        //启动两个线程分别调用method1()和method2()
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                method1();
            }
        });
        t1.start();
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                 method2();
            }
        });
        t2.start();
    }
}

运行结果:

Thread-0 enter method1
Thread-1 enter method2
Thread-0 enter method2
Thread-1 enter method1

method1()和method2()方法都没有打印退出,Thread1和Thread2分别占有key1和key2的情况下申请key2和key1。满足产生死锁的四个条件。状态如下图:
死锁

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GD_Hacker/article/details/80347415
个人分类: Java
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭