首页
编程随笔
Java笔记
Html/Css/Js
Android
后端笔记
服务器搭建
BUG收集
Java异常
Android异常
在线工具
Json格式化
编码/解码
Epub在线编辑
登录
发布文章
个人文章
退出登录
首页
技术教程
BUG收集
在线工具
资源下载
登录
发布文章
退出登录
搜索
当前位置:
首页
-
博客
- 正文
关闭
java死锁以及定位
更新时间:2022-03-20 16:34:08
阅读数:576
发布者:落幕
1、演示死锁代码如下 ```java import java.util.concurrent.TimeUnit; public class DeadLock { static class MyRunnable implements Runnable { String lockA; String lockB; public MyRunnable(String lockA, String lockB) { this.lockA = lockA; this.lockB = lockB; } @Override public void run() { synchronized (lockA) { System.out.println(Thread.currentThread().getName() + "获得锁" + lockA); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println(Thread.currentThread().getName() + "获得锁" + lockB); } } } } public static void main(String[] args) { // 为了方便演示使用了String作为锁,一般不建议使用String作为锁 String lockA = "lockA"; String lockB = "lockB"; new Thread(new MyRunnable(lockA, lockB), "ThreadA").start(); new Thread(new MyRunnable(lockB, lockA), "ThreadA").start(); } } ``` 控制台打印: ThreadA获得锁lockA ThreadA获得锁lockB 2、解决排查 使用jps查询java后台进程 >jps -l 18820 org.jetbrains.idea.maven.server.RemoteMavenServer36 20596 lock.DeadLock 20708 21348 org.jetbrains.jps.cmdline.Launcher 22420 sun.tools.jps.Jps 使用jstack检查对应程序是否有死锁 >jstack 20596 2022-03-20 16:27:50 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode): "DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x0000000003492800 nid=0x3884 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "ThreadA" #13 prio=5 os_prio=0 tid=0x000000001ea07800 nid=0x4dd8 waiting for monitor entry [0x000000002074e000] java.lang.Thread.State: BLOCKED (on object monitor) at lock.DeadLock$MyRunnable.run(DeadLock.java:24) - waiting to lock <0x000000076b684ba0> (a java.lang.String) - locked <0x000000076b684bd8> (a java.lang.String) at java.lang.Thread.run(Thread.java:748) "ThreadA" #12 prio=5 os_prio=0 tid=0x000000001ea06800 nid=0x5078 waiting for monitor entry [0x000000002064f000] java.lang.Thread.State: BLOCKED (on object monitor) at lock.DeadLock$MyRunnable.run(DeadLock.java:24) - waiting to lock <0x000000076b684bd8> (a java.lang.String) - locked <0x000000076b684ba0> (a java.lang.String) at java.lang.Thread.run(Thread.java:748) "Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001e9e6000 nid=0x513c runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001e944800 nid=0x4294 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e941800 nid=0x46f8 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001e940000 nid=0x4344 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001e93c000 nid=0x570c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e92f800 nid=0x24a0 runnable [0x000000001ff4e000] 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:171) 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 <0x000000076b7cee48> (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 <0x000000076b7cee48> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48) "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e886000 nid=0x2e64 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e8e2000 nid=0x4714 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001e863000 nid=0x5220 in Object.wait() [0x000000001fbde000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b508ed0> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x000000076b508ed0> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000000358a000 nid=0x4840 in Object.wait() [0x000000001fade000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b506bf8> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x000000076b506bf8> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=2 tid=0x000000001e842800 nid=0x44fc runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000034a8000 nid=0x2744 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000034aa000 nid=0x37e0 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000034ab800 nid=0x314c runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000034ad000 nid=0x3e48 runnable "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000034b0000 nid=0x27a8 runnable "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000034b1800 nid=0x33cc runnable "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000034b4800 nid=0x5248 runnable "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000034b5800 nid=0x2994 runnable "GC task thread#8 (ParallelGC)" os_prio=0 tid=0x00000000034b7000 nid=0x175c runnable "GC task thread#9 (ParallelGC)" os_prio=0 tid=0x00000000034b8000 nid=0x1acc runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000001e9f9000 nid=0x21a8 waiting on condition JNI global references: 12 Found one Java-level deadlock: ============================= "ThreadA": waiting to lock monitor 0x000000001d1a0d58 (object 0x000000076b684ba0, a java.lang.String), which is held by "ThreadA" "ThreadA": waiting to lock monitor 0x000000001d1a33d8 (object 0x000000076b684bd8, a java.lang.String), which is held by "ThreadA" Java stack information for the threads listed above: =================================================== "ThreadA": at lock.DeadLock$MyRunnable.run(DeadLock.java:24) - waiting to lock <0x000000076b684ba0> (a java.lang.String) - locked <0x000000076b684bd8> (a java.lang.String) at java.lang.Thread.run(Thread.java:748) "ThreadA": at lock.DeadLock$MyRunnable.run(DeadLock.java:24) - waiting to lock <0x000000076b684bd8> (a java.lang.String) - locked <0x000000076b684ba0> (a java.lang.String) at java.lang.Thread.run(Thread.java:748) Found 1 deadlock.