Java 死锁 DeadLock

死锁

  • 多个线程各自占有一些共享资源,并且互相等待其他线程占有的资源,才能运行,从而导致两个或多个线程都在等待对方释放资源,都停止执行的情形。
  • 某一个同步块,同时拥有“两个以上对象的锁”时,就可能会发生死锁问题

产生死锁的四个必要条件:

  1. 互斥条件:一个资源每次只能被一个进程使用
  2. 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放
  3. 不剥夺条件:进程已获得的资源,在未使用完之前,不能强行剥夺
  4. 循环等待条件:若干进程之间形成一种头尾相接 的循环等待资源关系
package com.syn;

public class DeadLock {
    public static void main (String[] args) {
        MakeUp g1 = new MakeUp (0,"灰姑凉");
        MakeUp g2 = new MakeUp (1,"白雪公主");

        g1.start ();
        g2.start ();
    }
}

class Lipstick{

}

class Mirror{

}

class MakeUp extends Thread{
    //需要的资源只有一份,用static确保只有一份
    static Mirror mirror = new Mirror();
    static Lipstick lipstick = new Lipstick ();
    int choice;//选择
    String girlName;//使用化妆品的人

    public MakeUp (int choice, String girlName) {
        this.choice = choice;
        this.girlName = girlName;
    }

    @Override
    public void run () {
        makeUP ();
    }
    private void makeUP(){
       if (choice==0){
           synchronized (lipstick){
               System.out.println (this.girlName+"获得了口红的锁");
               try {
                   Thread.sleep (1000);
               } catch (InterruptedException e) {
                   e.printStackTrace ();
               }
               synchronized (mirror){
               System.out.println (this.girlName+"获得了镜子的锁");
               }
           }
       }else {
           synchronized (mirror){
               System.out.println (this.girlName+"获得了镜子的锁");
               try {
                   Thread.sleep (2000);
               } catch (InterruptedException e) {
                   e.printStackTrace ();
               }
               synchronized (lipstick){
                   System.out.println (this.girlName+"获得了口红的锁");
               }
           }
       }
    }
}

避免死锁的方法

  • 只要想办法破除死锁四个必要条件中的一个或多个条件就可以避免死锁发生
package com.syn;

public class DeadLock {
    public static void main (String[] args) {
        MakeUp g1 = new MakeUp (0,"灰姑凉");
        MakeUp g2 = new MakeUp (1,"白雪公主");

        g1.start ();
        g2.start ();
    }
}

class Lipstick{

}

class Mirror{

}

class MakeUp extends Thread{
    //需要的资源只有一份,用static确保只有一份
    static Mirror mirror = new Mirror();
    static Lipstick lipstick = new Lipstick ();
    int choice;//选择
    String girlName;//使用化妆品的人

    public MakeUp (int choice, String girlName) {
        this.choice = choice;
        this.girlName = girlName;
    }

    @Override
    public void run () {
        makeUP ();
    }
    private void makeUP(){
       if (choice==0){
           synchronized (lipstick){
               System.out.println (this.girlName+"获得了口红的锁");
               try {
                   Thread.sleep (1000);
               } catch (InterruptedException e) {
                   e.printStackTrace ();
               }
           }
               synchronized (mirror){
               System.out.println (this.girlName+"获得了镜子的锁");
               }
       }else {
           synchronized (mirror){
               System.out.println (this.girlName+"获得了镜子的锁");
               try {
                   Thread.sleep (2000);
               } catch (InterruptedException e) {
                   e.printStackTrace ();
               }
           }
               synchronized (lipstick){
                   System.out.println (this.girlName+"获得了口红的锁");
               }
       }
    }
}

你可能感兴趣的:(java,开发语言)