手撕死锁代码

0. 死锁基础知识

  1. 多个线程同时被阻塞,它们中的⼀个或者全部都在等待某个资源被释放。由于线程被⽆限期地阻塞,因此程序不可能正常终⽌。
  2. 如何避免线程死锁?
    在这里插入图片描述

1. 产生死锁代码1

public class DeadLock {

    public static void main(String[] args) {
        dataSource da = new dataSource();

        //开启线程A,先获取A锁,在获取B锁
        new Thread(() -> {
            try {
                da.getLockA();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "线程A").start();
        //开启线程B,先获取B锁,在获取A锁
        new Thread(() -> {
            try {
                da.getLockB();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "线程B").start();
    }
}
    //资源类
class dataSource{

    private String lockA = "A锁";
    private String lockB = "B锁";

    //getLockA()方法先获取A锁,在获取B锁
    public void getLockA() throws InterruptedException {
        synchronized (lockA){
            System.out.println(Thread.currentThread().getName() + "已经获取" +lockA);
            Thread.sleep(1000); //获取A锁后,睡眠1秒钟,让getLockB()方法获取B锁
            System.out.println(Thread.currentThread().getName() + "尝试获取" +lockB);
            synchronized (lockB){
                System.out.println(Thread.currentThread().getName() + "已经获取" +lockB);
            }
        }
    }
    //getLockB()方法先获取B锁,在获取A锁
    public void getLockB() throws InterruptedException {
        synchronized (lockB){
            System.out.println(Thread.currentThread().getName() + "已经获取" +lockB);
            Thread.sleep(1000);//获取B锁后,睡眠1秒钟,让getLockA()方法获取A锁
            System.out.println(Thread.currentThread().getName() + "尝试获取" +lockA);
            synchronized (lockA){
                System.out.println(Thread.currentThread().getName() + "已经获取" +lockA);
            }
        }
    }
}

2. 运行结果2

在这里插入图片描述

3.产生死锁代码2

public class DeadLockDemo {
    private static Object resource1 = new Object();//资源 1
    private static Object resource2 = new Object();//资源 2
    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (resource1) {
                System.out.println(Thread.currentThread() + "get resource1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting get
                        resource2");
                synchronized (resource2) {
                    System.out.println(Thread.currentThread() + "get
                            resource2");
                }
            }
        }, "线程 1").start();
        new Thread(() -> {
            synchronized (resource2) {
                System.out.println(Thread.currentThread() + "get resource2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread() + "waiting get
                        resource1");
                synchronized (resource1) {
                    System.out.println(Thread.currentThread() + "get
                            resource1");
                }
            }
        }, "线程 2").start();
    }
}

4. 运行结果2

在这里插入图片描述

5. 避免死锁 修改线程2(靠按序申请资源来预防)

new Thread(() -> {
	    synchronized (resource1) {
	         System.out.println(Thread.currentThread() + "get resource1");
	         try {
	             Thread.sleep(1000);
	         } catch (InterruptedException e) {
	             e.printStackTrace();
	         }
	         System.out.println(Thread.currentThread() + "waiting get
	                 resource2");
	         synchronized (resource2) {
	             System.out.println(Thread.currentThread() + "get
	                     resource2");
	         }
	     }
	 }, "线程 2").start();

6. 运行结果3

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值