zoukankan      html  css  js  c++  java
  • 多线程系列教材 (五)- Java 演示多线程死锁

    当业务比较复杂,多线程应用里有可能会发生死锁

    步骤1:演示死锁
    步骤2:练习-死锁
    步骤3:答案-死锁

    步骤 1 : 演示死锁

    1. 线程1 首先占有对象1,接着试图占有对象2
    2. 线程2 首先占有对象2,接着试图占有对象1
    3. 线程1 等待线程2释放对象2
    4. 与此同时,线程2等待线程1释放对象1
    就会。。。一直等待下去,直到天荒地老,海枯石烂,山无棱 ,天地合。。。

    演示死锁

    package multiplethread;

       

    import charactor.Hero;

        

    public class TestThread {

          

        public static void main(String[] args) {

            final Hero ahri = new Hero();

            ahri.name = "九尾妖狐";

            final Hero annie = new Hero();

            annie.name = "安妮";

             

            Thread t1 = new Thread(){

                public void run(){

                    //占有九尾妖狐

                    synchronized (ahri) {

                        System.out.println("t1 已占有九尾妖狐");

                        try {

                            //停顿1000毫秒,另一个线程有足够的时间占有安妮

                            Thread.sleep(1000);

                        catch (InterruptedException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                        }

                         

                        System.out.println("t1 试图占有安妮");

                        System.out.println("t1 等待中 。。。。");

                        synchronized (annie) {

                            System.out.println("do something");

                        }

                    }   

                     

                }

            };

            t1.start();

            Thread t2 = new Thread(){

                public void run(){

                    //占有安妮

                    synchronized (annie) {

                        System.out.println("t2 已占有安妮");

                        try {

                             

                            //停顿1000毫秒,另一个线程有足够的时间占有暂用九尾妖狐

                            Thread.sleep(1000);

                        catch (InterruptedException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                        }

                        System.out.println("t2 试图占有九尾妖狐");

                        System.out.println("t2 等待中 。。。。");

                        synchronized (ahri) {

                            System.out.println("do something");

                        }

                    }   

                     

                }

            };

            t2.start();

       }

            

    }


    更多内容,点击了解: https://how2j.cn/k/thread/thread-deadlock/356.html

  • 相关阅读:
    深入解读kubernetes网络基本原理
    Go!Go!Go!设计模式-组合设计模式
    Go!Go!Go!设计模式-创建型模式(简单工厂,工厂方法,抽象工厂)
    Linux内核之磁盘和分区
    Docker容器网络基础
    chart仓库之创建-入库-使用(helm,helm-push,chartmuseum)
    Go语言完整解析Go!Go!Go!(一)数据类型 之 Channel & Goroutine
    k8s爬坑集锦[网络问题]-服务无法访问
    数字证书的原理与应用&爬坑
    ingress的用法与原理
  • 原文地址:https://www.cnblogs.com/Lanht/p/12615474.html
Copyright © 2011-2022 走看看