首页 > 编程语言 >java死锁(Java-level deadlock)

java死锁(Java-level deadlock)

时间:2022-08-24 21:23:58浏览次数:87  
标签:lang java Thread prio nid 死锁 Java deadlock os

java-level deadlock

如下代码可以模拟java死锁。注意:当出现死锁时,应用程序是无响应的。错误信息:

Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x000000001c773158 (object 0x000000076bbc06b8, a java.util.concurrent.Concurren
tHashMap),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x000000001c774548 (object 0x000000076bbc06f8, a java.lang.Object),
which is held by "Thread-1"

 

deadlock demo及deadlock信息

上代码:

import java.util.concurrent.ConcurrentHashMap;
import static java.util.concurrent.TimeUnit.SECONDS;

public class TestDeadlockMain {

    private static final ConcurrentHashMap<String, String> concurrentHashMap1 = new ConcurrentHashMap<>(128);
    private static final Object concurrentHashMap2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (concurrentHashMap1) {

                // 模拟逻辑执行
                try {
                    SECONDS.sleep(1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                synchronized (concurrentHashMap2) {
                    // 模拟逻辑执行
                    System.out.println("test deadlock");
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (concurrentHashMap2) {
                synchronized (concurrentHashMap1) {
                    // 模拟逻辑执行
                    try {
                        SECONDS.sleep(1L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

}

 

 

运行程序,会发现一直无响应,一直处于停顿状态。

这时呢, 在IDEA的Terminal窗口,执行jps -l 命令,找到当前执行的java程序进程pid。 接下来执行 jstack pid 命令,查看JVM里的线程快照信息。

如下贴出来Terminal窗口的内容:

D:\SourceProject\account\target\test-classes\com\emaxcard\jvm>jps -l
24912
21556 org.jetbrains.jps.cmdline.Launcher
28840 com.emaxcard.jvm.TestDeadlockMain
30328 sun.tools.jps.Jps
13644 org.jetbrains.idea.maven.server.RemoteMavenServer

D:\SourceProject\account\target\test-classes\com\emaxcard\jvm>jstack 28840
2022-08-24 20:56:11
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.40-b25 mixed mode):

"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x0000000002a58800 nid=0x6bf4 waiting on condition [0x0000000000
000000]
   java.lang.Thread.State: RUNNABLE

"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001fdba800 nid=0x4f88 waiting for monitor entry [0x0000000020
e1f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.emaxcard.jvm.TestDeadlockMain.lambda$main$1(TestDeadlockMain.java:41)
        - waiting to lock <0x000000076bbc06b8> (a java.util.concurrent.ConcurrentHashMap)
        - locked <0x000000076bbc06f8> (a java.lang.Object)
        at com.emaxcard.jvm.TestDeadlockMain$$Lambda$2/654845766.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

"Thread-0" #12 prio=5 os_prio=0 tid=0x000000001fcf8000 nid=0x6ecc waiting for monitor entry [0x0000000020
d1e000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at com.emaxcard.jvm.TestDeadlockMain.lambda$main$0(TestDeadlockMain.java:33)
        - waiting to lock <0x000000076bbc06f8> (a java.lang.Object)
        - locked <0x000000076bbc06b8> (a java.util.concurrent.ConcurrentHashMap)
        at com.emaxcard.jvm.TestDeadlockMain$$Lambda$1/2101842856.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001f53d800 nid=0x72d0 runnable [0x00000000000000
00]
   java.lang.Thread.State: RUNNABLE

"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001f4ea800 nid=0x948 waiting on condition [0
x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001f4e8000 nid=0x5ed0 waiting on condition [0
x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001f4e3000 nid=0x6130 waiting on condition [0
x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001f4e0800 nid=0x6804 waiting on condition [0
x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001f4a8000 nid=0x52bc runnable [0x000000001f6
4e000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
        at java.net.SocketInputStream.read(SocketInputStream.java:170)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        - locked <0x000000076bce5ac8> (a java.io.InputStreamReader)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.fill(BufferedReader.java:161)
        at java.io.BufferedReader.readLine(BufferedReader.java:324)
        - locked <0x000000076bce5ac8> (a java.io.InputStreamReader)
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
        at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:61)

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e7d2000 nid=0x6478 waiting on condition [0x00
00000000000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e7d1800 nid=0x5e4c runnable [0x000000000000
0000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c774800 nid=0x5df0 in Object.wait() [0x000000001e73
e000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076b306f58> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x000000076b306f58> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001c76e000 nid=0x6828 in Object.wait() [0x000
000001e63f000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x000000076b306998> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
        - locked <0x000000076b306998> (a java.lang.ref.Reference$Lock)

"VM Thread" os_prio=2 tid=0x000000001c76a000 nid=0x3384 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002a72000 nid=0x6d44 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002a73800 nid=0x61c8 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002a75000 nid=0x6a00 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002a77800 nid=0x5e6c runnable

"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002a7a000 nid=0x480c runnable

"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002a7b000 nid=0x497c runnable

"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002a7e000 nid=0x13f8 runnable

"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002a7f800 nid=0x40b4 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x000000001f53e800 nid=0x7314 waiting on condition

JNI global references: 330


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x000000001c773158 (object 0x000000076bbc06b8, a java.util.concurrent.Concurren
tHashMap),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x000000001c774548 (object 0x000000076bbc06f8, a java.lang.Object),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
        at com.emaxcard.jvm.TestDeadlockMain.lambda$main$1(TestDeadlockMain.java:41)
        - waiting to lock <0x000000076bbc06b8> (a java.util.concurrent.ConcurrentHashMap)
        - locked <0x000000076bbc06f8> (a java.lang.Object)
        at com.emaxcard.jvm.TestDeadlockMain$$Lambda$2/654845766.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
"Thread-0":
        at com.emaxcard.jvm.TestDeadlockMain.lambda$main$0(TestDeadlockMain.java:33)
        - waiting to lock <0x000000076bbc06f8> (a java.lang.Object)
        - locked <0x000000076bbc06b8> (a java.util.concurrent.ConcurrentHashMap)
        at com.emaxcard.jvm.TestDeadlockMain$$Lambda$1/2101842856.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

 

 

jstack简介

  1. jstack命令用于打印指定Java进程、核心文件或远程调试服务器的Java线程的Java堆栈跟踪信息[1]
  2. jstack命令可以生成JVM当前时刻的线程快照。线程快照是当前JVM内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等。
  3. 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。
  4. 当指定的进程在64位Java虚拟机上运行时,可能需要指定-J-d64选项,例如:jstack -J-d64 -m pid。
  5. 该命令可能在未来的版本中不可用!!!

语法

jstack [ options ] pid

标签:lang,java,Thread,prio,nid,死锁,Java,deadlock,os
From: https://www.cnblogs.com/buguge/p/16622318.html

相关文章

  • JavaScript知识-简介、语言规范、语言基础
    目录JavaScript简介1.ECMAScript和JavaScript的关系2.JavaScript语言3.HTML、CSS、JavaScriptJavaScript基础之语言规范1.如何使用?2.注释3.结束符JavaScript语言基础1.Java......
  • day25--Java集合08
    Java集合0815.HashTable15.1HashTable的基本介绍存放的元素是键值对:即K-VHashTable的键和值都不能为nullHashTable的使用方法基本上和HashMap一样HashTable是线程安......
  • java学习笔记014 枚举类
    1.比较器Comparable接口自然排序 @Override publicintcompareTo(Tt){ //自定义比较规则 //要求:this>t,返回正整数 // this==t,返回0 // this<t,返......
  • javaWeb-HTML
    HTML基础标签字体大小(<h1>---<h6>)<h1>我是h1</h1><h2>我是h1</h2><h3>我是h1</h3><h4>我是h1</h4><h5>我是h1</h5><h6>我是h1</h6>换行<hr>......
  • jpaJAVASql
    QCarInfoqCarInfo=QCarInfo.qCarInfo;BooleanExpressionexpression=qCarInfo.valid.eq(true);if(StringUtils.isNotEmpty(carListPCInput......
  • java关于程序流程控制
    java流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:顺序结构分支结构循环结构 顺序结构:程序从上到下逐行地执行,中间没有任何判断和跳转。分支结构:根......
  • HTML编辑器粘贴图片自动上传到服务器(Java版)
    ​如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-......
  • java泛型详解
    java泛型详解1.泛型​ Java泛型是J2SE1.5中引入的一个新特性,其本质是参数化类型,也就是说所操作的数据类型被指定为一个参数(typeparameter)这种参数类型可以用在类、接......
  • DES加密解密:android、java、js
    需求:登录的时候WEB或APP将数据加密后传给JAVA后端,后端接收到数据解密后得到数据进行处理。eg:明文:12345678密文:PofrPuMcG5CiXuyR5B5ysQ==一、java端importjavax.cr......
  • abap转java时间戳
    sap与java系统交互时,日期、时间转换时间戳可以使用下面方法实现: 时间戳生成cl_pco_utility=>convert_abap_timestamp_to_java  时间戳解析cl_pco_utility=>conv......