欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java篇——多线程(死锁)

程序员文章站 2022-04-15 19:17:10
死锁避免方法◆产生死锁的四个必要条件:1.互斥条件:一个资源每次只能被一个进程使用。2.请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。3.不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。4.循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。产生死锁例子package Thread_test;public class DeadLock { public static void main(String[] args) {...

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


产生死锁例子

线程A拿到了资源A需要获取资源B,线程B拿到了资源B需要获取资源A,当两个线程都在等待资源时,就出现死锁现象。

package Thread_test;

public class DeadLock {
    public static void main(String[] args) {
        MakeUp girl1 = new MakeUp(0, "灰姑娘");
        MakeUp girl2 = new MakeUp(1, "白雪公主");
        girl1.start();
        girl2.start();
    }
}

class Lipstick {
    //口红
}

class Mirror {
    //镜子
}

class MakeUp extends Thread {
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();
    int choice;
    String getName;

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

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lipstick) {
                System.out.println(this.getName + "获得口红的锁");
                Thread.sleep(1000);
                synchronized (mirror) {
                    System.out.println(this.getName + "获得镜子的锁");
                }
            }

        } else {
            synchronized (mirror) {
                System.out.println(this.getName + "获得镜子的锁");
                Thread.sleep(1000);
                synchronized (lipstick) {
                    System.out.println(this.getName + "获得口红的锁");
                }
            }

        }


    }
}

修改之后(解除上述第二个条件)

package Thread_test;

public class DeadLock {
    public static void main(String[] args) {
        MakeUp girl1 = new MakeUp(0, "灰姑娘");
        MakeUp girl2 = new MakeUp(1, "白雪公主");
        girl1.start();
        girl2.start();
    }
}

class Lipstick {
    //口红
}

class Mirror {
    //镜子
}

class MakeUp extends Thread {
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();
    int choice;
    String getName;

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

    @Override
    public void run() {
        try {
            makeup();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void makeup() throws InterruptedException {
        if (choice == 0) {
            synchronized (lipstick) {
                System.out.println(this.getName + "获得口红的锁");
                Thread.sleep(1000);
            }
            synchronized (mirror) {
                System.out.println(this.getName + "获得镜子的锁");
            }

        } else {
            synchronized (mirror) {
                System.out.println(this.getName + "获得镜子的锁");
                Thread.sleep(1000);
            }
            synchronized (lipstick) {
                System.out.println(this.getName + "获得口红的锁");
            }
        }
    }
}

本文地址:https://blog.csdn.net/weixin_47016497/article/details/107596216

相关标签: 多线程 java