java两种方式实现死锁

产生死锁的四个必要条件:
(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。
(一)对于第二种情况


public class LockTest implements Runnable {

    private Object o1 = new Object();  
    private Object o2 = new Object();  
    private boolean flag=true;  

public void run() {  

        if (flag) {   
            flag=false; 
            synchronized (o1) {  
                System.out.println(Thread.currentThread().getName() + " have o1");  
                try {  
                    Thread.sleep(100);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                synchronized (o2) {  
                    System.out.println(Thread.currentThread().getName() + " have o2");  
                }  
            }  
        } else {  
            flag=true; 
            synchronized (o2) {  
                System.out.println(Thread.currentThread().getName() + " have o2");  
                try {  
                    Thread.sleep(100);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                synchronized (o1) {  
                    System.out.println(Thread.currentThread().getName() + " have o1");  
                }  
            }  
        }  
    }   

public static void main(String[] args) {  
   LockTest b1= new LockTest();  
   Thread thread1=new Thread(b1);
   Thread thread2=new Thread(b1);
   thread1.start();
   thread2.start();
    }
}

(二)对于第四种情况


public class LockTest implements Runnable {

    private Object o1 = new Object();  
    private Object o2 = new Object();  
    private Object o3 = new Object();  
    private int flag=1;  

public void run() {  

        if (flag==1) {   
            flag=2; 
            synchronized (o1) {  
                System.out.println(Thread.currentThread().getName() + " have o1");  
                try {  
                    Thread.sleep(100);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                synchronized (o2) {  
                    System.out.println(Thread.currentThread().getName() + " have o2");  
                }  
            }  
        } else if(flag==2) {  
            flag=3; 
            synchronized (o2) {  
                System.out.println(Thread.currentThread().getName() + " have o2");  
                try {  
                    Thread.sleep(100);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                synchronized (o3) {  
                    System.out.println(Thread.currentThread().getName() + " have o3");  
                }  
            }  
        }  
        else  {  
            flag=1; 
            synchronized (o3) {  
                System.out.println(Thread.currentThread().getName() + " have o3");  
                try {  
                    Thread.sleep(100);  
                } catch (InterruptedException e) {  
                    e.printStackTrace();  
                }  
                synchronized (o1) {  
                    System.out.println(Thread.currentThread().getName() + " have o1");  
                }  
            }  
        }  
    }   

public static void main(String[] args) {  
   LockTest b1= new LockTest();  
   Thread thread1=new Thread(b1);
   Thread thread2=new Thread(b1);
   Thread thread3=new Thread(b1);
   thread1.start();
   thread2.start();
   thread3.start();
    }
}

例子不难看懂,面试的时候有可能要求当场就要写。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,死锁是指多个线程相互等待对方释放锁,导致所有线程都无法继续执行的一种情况。下面举一个简单的例子说明如何实现一个死锁: ```java public class Deadlock { private static Object lock1 = new Object(); private static Object lock2 = new Object(); public static void main(String[] args) { Thread thread1 = new Thread(() -> { synchronized (lock1) { System.out.println("Thread 1 acquired lock1"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock2) { System.out.println("Thread 1 acquired lock2"); } } }); Thread thread2 = new Thread(() -> { synchronized (lock2) { System.out.println("Thread 2 acquired lock2"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock1) { System.out.println("Thread 2 acquired lock1"); } } }); thread1.start(); thread2.start(); } } ``` 在这个例子中,我们定义了两个对象锁lock1和lock2,并通过两个线程thread1和thread2分别获取这两个锁。当thread1获取到lock1锁之后,尝试获取lock2锁,而此时lock2已经被thread2获取,因此thread1会一直等待lock2的释放。而thread2同样也会等待thread1释放lock1锁。这样就形成了一个死锁,所有线程都无法继续执行。 需要注意的是,死锁是一种非常危险的情况,会导致应用程序的崩溃和数据丢失。因此,在编写多线程程序时,需要注意避免死锁的发生,例如避免多个线程同时获取多个锁、按照相同的顺序获取锁等等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值