• 2.2.12多线程的死锁


    java线程死锁是一个经典的多线程问题,因为不同的线程都在等待根本不可能释放的锁,从而导致所有的任务无法继续完成,在多线程技术中,死锁是必须避免的,因为会造成线程的假死。

    package com.cky.thread;
    
    /**
     * Created by edison on 2017/12/9.
     */
    public class DeadThread implements  Runnable{
        public String username;
        public Object object1 = new Object();
        public Object object2 = new Object();
        public void setFlag(String username) {
            this.username = username;
        }
        @Override
        public void run() {
            if (username.equals("a")) {
                synchronized (object1) {
                    try {
                        System.out.println("username="+username);
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    synchronized (object2) {
                        System.out.println("按lock1-》lock2代码顺序执行了");
                    }
                }
    
    
    
            }
    
            if (username.equals("b")) {
                synchronized (object2) {
                    try {
                        System.out.println("username="+username);
                        Thread.sleep(2000);
    
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    synchronized (object1) {
                        System.out.println("按lock2-》lock1代码顺序执行了");
                    }
                }
            }
    
        }
    }
    package com.cky.test;
    
    import com.cky.thread.DeadThread;
    
    /**
     * Created by edison on 2017/12/9.
     */
    public class Run {
        public static void main(String[] args) {
            try {
                DeadThread t1 = new DeadThread();
                t1.setFlag("a");
                Thread thread1 = new Thread(t1);
                thread1.start();
                Thread.sleep(100);
                t1.setFlag("b");
                Thread t2 = new Thread(t1);
                t2.start();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
    
        }
    }
    username=a
    username=b

    程序运行结果如上。

     可以用jdk自带的工具来监测是否有死锁现象

    C:Usersedison>cd C:itsoftjdkin
    
    C:itsoftjdkin>jps
    18016 Launcher
    14392 Jps
    20696
    4936 AppMain
    C:itsoftjdkin>jstack -l 4936
    2017-12-09 13:49:50
    Full thread dump Java HotSpot(TM) Client VM (25.144-b01 mixed mode, sharing):
    =============================
    "Thread-1":
      waiting to lock monitor 0x00d7ad94 (object 0x04d01230, a java.lang.Object),
      which is held by "Thread-0"
    "Thread-0":
      waiting to lock monitor 0x00d793c4 (object 0x04d01238, a java.lang.Object),
      which is held by "Thread-1"
    
    Java stack information for the threads listed above:
    ===================================================
    "Thread-1":
            at com.cky.thread.DeadThread.run(DeadThread.java:44)
            - waiting to lock <0x04d01230> (a java.lang.Object)
            - locked <0x04d01238> (a java.lang.Object)
            at java.lang.Thread.run(Thread.java:748)
    "Thread-0":
            at com.cky.thread.DeadThread.run(DeadThread.java:25)
            - waiting to lock <0x04d01238> (a java.lang.Object)
            - locked <0x04d01230> (a java.lang.Object)
            at java.lang.Thread.run(Thread.java:748)
    
    Found 1 deadlock.

    监测出现死锁

    死锁是程序设计的bug,在程序设计时就要避免双方互相持有对方锁的情况,需要说明的是,本实验使用synchronized嵌套的代码结构来实现死锁,

    其实不用嵌套的synchronized代码结构也会出现死锁,与是否嵌套无任何关系,不要被代码结构所误导,只要互相等待双方释放就有可能出现死锁。

  • 相关阅读:
    迭代器概念与traits编程技法
    C++模板的特化与偏特化
    c++ operator关键字
    cookie-小总结吧
    ping
    git
    setTimeout()基础/setInterval()基础
    SASS
    命令行编译sass
    sublime添加sass编译
  • 原文地址:https://www.cnblogs.com/edison20161121/p/8011168.html
Copyright © 2020-2023  润新知