2.2.12多线程的死锁

Posted 成功的路上总是离不开贵人的帮助,名师的指点和小人的刺激。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:\Users\edison>cd C:\itsoft\jdk\bin

C:\itsoft\jdk\bin>jps
18016 Launcher
14392 Jps
20696
4936 AppMain
C:\itsoft\jdk\bin>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代码结构也会出现死锁,与是否嵌套无任何关系,不要被代码结构所误导,只要互相等待双方释放就有可能出现死锁。

以上是关于2.2.12多线程的死锁的主要内容,如果未能解决你的问题,请参考以下文章

怎么处理JAVA多线程死锁问题?

Java多线程-死锁简述

死锁的概念以及处理方式

多线程的死锁

JAVA程序设计,多线程且避免死锁

多线程死锁代码