zoukankan      html  css  js  c++  java
  • 死锁

    概述

    互相要对方正在占有的锁,导致卡住不动

    实例

    /**
     * 死锁
     */
    public class TestDeadLock {
        public static void main(String[] args) {
            new MakeUp(0,"嘟嘟").start();
            new MakeUp(1,"dudu").start();
        }
    }
    //镜子
    class Mirror{
    
    }
    //口红
    class Lipstick{
    
    }
    //化妆
    class MakeUp extends Thread{
        static Mirror mirror = new Mirror();
        static Lipstick lipstick = new Lipstick();
        private int select;
        private String name;
    
        public MakeUp(int select, String name) {
            this.select = select;
            this.name = name;
        }
    
        @Override
        public void run() {
            try {
                makeup();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public void makeup() throws InterruptedException {
            if(select == 0){
                synchronized (mirror){
                    System.out.println(this.name+  "   获得了镜子的锁");
                    Thread.sleep(2000);
                    synchronized (lipstick){
                        System.out.println(this.name + "  获得了口红的锁");
                    }
                }
            }else{
                synchronized (lipstick){
                    System.out.println(this.name + "  获得了口红的锁");
                    Thread.sleep(2000);
                    synchronized (mirror){
                        System.out.println(this.name+  "   获得了镜子的锁");
                    }
                }
            }
        }
    
  • 相关阅读:
    python自动华 (十二)
    python自动华 (十一)
    python自动华 (十)
    python自动华 (八)
    python自动华 (九)
    python自动华 (七)
    python自动华 (六)
    数据
    页面自适应
    判断是否移动端
  • 原文地址:https://www.cnblogs.com/gbhh/p/13768106.html
Copyright © 2011-2022 走看看