Java写一个简单的死锁程序

Java死锁程序

public class DeadLock {
    public static void main(String[] args) {
        new Thread(() -> {
            try {
                System.out.println("thread1 开始运行");
                synchronized (DeadLock.class) {
                    System.out.println("thread1 获取 DeadLock.class对象 锁'");

                    Thread.sleep(1000); // 让出CPU,让thread2先获得Object.class锁
                    synchronized (Object.class) {
                        System.out.println("thread1 获取 Object.class对象 锁");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("thread1 结束!!");
        }).start();
        new Thread(() -> {
            try {
                System.out.println("thread2 开始运行");
                synchronized (Object.class) {
                    System.out.println("thread2 获取 Object.class对象 锁");

                   Thread.sleep(1000); // 让出CPU,让thread1先获得DeadLock.class锁
                    synchronized (DeadLock.class) {
                        System.out.println("thread2 获取 DeadLock.class对象 锁'");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("thread2 结束!!");
        }).start();
    }
}

运行结果:

thread1 开始运行
thread1 获取 DeadLock.class对象 锁'
thread2 开始运行
thread2 获取 Object.class对象 锁

为什么要有sleep?

因为,sleep可以让当前线程让出CPU,让另一个线程运行。
比如线程1 sleep的时候,它已经获得了DeadLock锁,它下一步要获得Object锁。
这时让出cpu,可以让线程2先获得Object的锁。这样线程1就得不到了。同时线程2还请求线程1得到的DeadLock锁,显然得不到!
造成了循环等待!

去掉线程里的两个sleep语句,死锁可能就没有了!(注意,只是可能没有)

public class DeadLock {
    public static void main(String[] args) {
        new Thread(() -> {
            try {
                System.out.println("thread1 开始运行");
                synchronized (DeadLock.class) {
                    System.out.println("thread1 获取 DeadLock.class对象 锁'");

                   // Thread.sleep(1000); // 让出CPU,让thread2先获得Object.class锁
                    synchronized (Object.class) {
                        System.out.println("thread1 获取 Object.class对象 锁");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("thread1 结束!!");
        }).start();
        new Thread(() -> {
            try {
                System.out.println("thread2 开始运行");
                synchronized (Object.class) {
                    System.out.println("thread2 获取 Object.class对象 锁");

                  //  Thread.sleep(1000); // 让出CPU,让thread1先获得DeadLock.class锁
                    synchronized (DeadLock.class) {
                        System.out.println("thread2 获取 DeadLock.class对象 锁'");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("thread2 结束!!");
        }).start();
    }
}

某一种运行结果:

thread1 开始运行
thread2 开始运行
thread2 获取 Object.class对象 锁
thread2 获取 DeadLock.class对象 锁'
thread2 结束!! // 线程2一次性执行完,释放所有锁!
thread1 获取 DeadLock.class对象 锁'
thread1 获取 Object.class对象 锁
thread1 结束!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值