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 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(); new Thread(()->{ while (true){ System.out.println(System.currentTimeMillis()); try { SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); }}
运行程序,通过控制台日志是无法看到死锁信息的。只能看到第三个线程在间隔1s不停的打日志,却无法看到另外两个线程在干啥。
如果是所调用的程序有用户界面的话,就会看到长时间无响应或超时的现象。
至于这个demo呢,我们可以借助IDEA的Terminal窗口。执行jps -l 命令,找到当前执行的java程序的进程pid。 接下来执行 jstack pid 命令,查看JVM里的线程快照信息。
如下贴出来Terminal窗口的内容:
D:\SourceProject\account\target\test-classes\com\emaxcard\jvm>jps -l2491221556 org.jetbrains.jps.cmdline.Launcher28840 com.emaxcard.jvm.TestDeadlockMain30328 sun.tools.jps.Jps13644 org.jetbrains.idea.maven.server.RemoteMavenServerD:\SourceProject\account\target\test-classes\com\emaxcard\jvm>jstack 288402022-08-24 20:56:11Full 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 [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Thread-1" #13 prio=5 os_prio=0 tid=0x000000001fdba800 nid=0x4f88 waiting for monitor entry [0x0000000020e1f000] java.lang.Thread.State: BLOCKED (on object monitor) at com.emaxcard.jvm.TestDeadlockMain.lambda$main$1(TestDeadlockMain.java:41) - waiting to lock (a java.util.concurrent.ConcurrentHashMap) - locked (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 [0x0000000020d1e000] java.lang.Thread.State: BLOCKED (on object monitor) at com.emaxcard.jvm.TestDeadlockMain.lambda$main$0(TestDeadlockMain.java:33) - waiting to lock (a java.lang.Object) - locked (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 [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001f4ea800 nid=0x948 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001f4e8000 nid=0x5ed0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001f4e3000 nid=0x6130 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001f4e0800 nid=0x6804 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001f4a8000 nid=0x52bc runnable [0x000000001f64e000] 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 (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 (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 [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e7d1800 nid=0x5e4c runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c774800 nid=0x5df0 in Object.wait() [0x000000001e73e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked (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() [0x000000001e63f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on (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 (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 conditionJNI global references: 330Found one Java-level deadlock:============================="Thread-1": waiting to lock monitor 0x000000001c773158 (object 0x000000076bbc06b8, a java.util.concurrent.ConcurrentHashMap), 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 (a java.util.concurrent.ConcurrentHashMap) - locked (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 (a java.lang.Object) - locked (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简介
- jstack命令用于打印指定Java进程、核心文件或远程调试服务器的Java线程的Java堆栈跟踪信息[1]。
- jstack命令可以生成JVM当前时刻的线程快照。线程快照是当前JVM内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等。
- 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。
- 当指定的进程在64位Java虚拟机上运行时,可能需要指定-J-d64选项,例如:jstack -J-d64 -m pid。
- 该命令可能在未来的版本中不可用!!!
语法
jstack [ options ] pid
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!–buguge
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/16622318.html
hr.signhr{width:80%;margin:0 auto;border: 0;height: 4px;background-image: linear-gradient(to right, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0))}