码迷,mamicode.com
首页 > 编程语言 > 详细

Java死锁演示

时间:2019-12-14 12:11:15      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:adl   vat   ted   线程   嵌套   sys   stat   evel   pack   

Java死锁演示

在线程中嵌套获取锁导致死锁。思路,尽量不要嵌套获取锁。

package com.mozq.demo.demo;

public class DeadLockDemo {
    private static String A = "A";
    private static String B = "B";
    public static void main(String[] args) {
        new DeadLockDemo().deadLock();
    }
    private void deadLock() {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (A) {
                    System.out.println("线程1获取A锁");
                    try { Thread.currentThread().sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (B) {
                        System.out.println("1");
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (B) {
                    synchronized (A) {
                        System.out.println("2");
                    }
                }
            }
        });
        t1.start();
        t2.start();
    }
}

控制台输出

Connected to the target VM, address: '127.0.0.1:18103', transport: 'socket'
线程1获取A锁

jstack dump进程信息

C:\Program Files\Java\jdk1.8.0_181\bin>jstack 196408 > d:/00/02/dump1.txt

C:\Program Files\Java\jdk1.8.0_181\bin>netstat -ano | findstr 18103
  TCP    127.0.0.1:18103        127.0.0.1:18104        ESTABLISHED     196408
  TCP    127.0.0.1:18104        127.0.0.1:18103        ESTABLISHED     204140

C:\Program Files\Java\jdk1.8.0_181\bin>jstack 204140 > d:/00/02/dump1.txt

死锁信息

"Thread-1" #14 prio=5 os_prio=0 tid=0x00000000196f1000 nid=0x33cdc waiting for monitor entry [0x000000001aaff000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.mozq.demo.demo.DeadLockDemo$2.run(DeadLockDemo.java:30)
    - waiting to lock <0x00000000d63716e0> (a java.lang.String)
    - locked <0x00000000d6371710> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:748)

"Thread-0" #13 prio=5 os_prio=0 tid=0x00000000196e7800 nid=0x33898 waiting for monitor entry [0x000000001a9fe000]
   java.lang.Thread.State: BLOCKED (on object monitor)
    at com.mozq.demo.demo.DeadLockDemo$1.run(DeadLockDemo.java:20)
    - waiting to lock <0x00000000d6371710> (a java.lang.String)
    - locked <0x00000000d63716e0> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:748)
Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x0000000017f3bcc8 (object 0x00000000d63716e0, a java.lang.String),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x0000000017f3fd68 (object 0x00000000d6371710, a java.lang.String),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
    at com.mozq.demo.demo.DeadLockDemo$2.run(DeadLockDemo.java:30)
    - waiting to lock <0x00000000d63716e0> (a java.lang.String)
    - locked <0x00000000d6371710> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:748)
"Thread-0":
    at com.mozq.demo.demo.DeadLockDemo$1.run(DeadLockDemo.java:20)
    - waiting to lock <0x00000000d6371710> (a java.lang.String)
    - locked <0x00000000d63716e0> (a java.lang.String)
    at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

bug

idea中运行main方法后,如何获取其对应的进程id?

Java死锁演示

标签:adl   vat   ted   线程   嵌套   sys   stat   evel   pack   

原文地址:https://www.cnblogs.com/mozq/p/12038452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!