龙空技术网

java程序死锁,3种方式快速找到死锁代码

程序人生a 54

前言:

今天兄弟们对“java惊魂”大约比较关心,各位老铁们都需要了解一些“java惊魂”的相关资讯。那么小编也在网上搜集了一些关于“java惊魂””的相关资讯,希望咱们能喜欢,我们快快来学习一下吧!

java程序中出现死锁问题,如果不了解排查方法,是束手无策的,今天咱们用三种方法找到死锁问题。

运行下面代码

package com.jvm.visualvm;/** * <a href="">Java干货铺子,只生产干货,公众号:javacode2018</a> */public class Demo4 { public static void main(String[] args) { User u1 = new User("u1"); User u2 = new User("u2"); Thread thread1 = new Thread(new SynAddRunalbe(u1, u2, 1, 2, true)); thread1.setName("thread1"); thread1.start(); Thread thread2 = new Thread(new SynAddRunalbe(u1, u2, 2, 1, false)); thread2.setName("thread2"); thread2.start(); } /** * 线程死锁等待演示 */ public static class SynAddRunalbe implements Runnable { User u1, u2; int a, b; boolean flag; public SynAddRunalbe(User u1, User u2, int a, int b, boolean flag) { this.u1 = u1; this.u2 = u2; this.a = a; this.b = b; this.flag = flag; } @Override public void run() { try { if (flag) { synchronized (u1) { Thread.sleep(100); synchronized (u2) { System.out.println(a + b); } } } else { synchronized (u2) { Thread.sleep(100); synchronized (u1) { System.out.println(a + b); } } } } catch (InterruptedException e) { e.printStackTrace(); } } } public static class User { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public User(String name) { this.name = name; } @Override public String toString() { return "User{" + "name='" + name + '\'' + '}'; } }}

程序中:thread1持有u1的锁,thread2持有u2的锁,thread1等待获取u2的锁,thread2等待获取u1的锁,相互需要获取的锁都被对方持有者,造成了死锁。程序运行中一直无法结束。

通过jdk工具jps、jstack排查死锁问题

jps、jstack都是jdk提供的命令工具,方便用户排查程序的一些问题。更详细的用法见文档最后。

步骤一:使用jsp查找程序进行

jps:jdk提供的一个工具,可以查看到正在运行的java进程

C:\Users\Think>jps -l5824 com.jvm.visualvm.Demo4

步骤二:使用jstack查看线程堆栈信息

jstack:jdk提供的一个工具,可以查看java进程中线程堆栈信息。更详细的用法见文档最后。

C:\Users\Think>jstack 58242019-06-04 15:34:37Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x0000000002cf4000 nid=0x9fd8 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"thread2" #13 prio=5 os_prio=0 tid=0x000000002884d800 nid=0x40c4 waiting for monitor entry [0x00000000292ce000] java.lang.Thread.State: BLOCKED (on object monitor) at com.jvm.visualvm.Demo4$SynAddRunalbe.run(Demo4.java:49) - waiting to lock <0x00000007173d4178> (a com.jvm.visualvm.Demo4$User) - locked <0x00000007173d41b8> (a com.jvm.visualvm.Demo4$User) at java.lang.Thread.run(Thread.java:745)"thread1" #12 prio=5 os_prio=0 tid=0x000000002885b800 nid=0x99c0 waiting for monitor entry [0x00000000291cf000] java.lang.Thread.State: BLOCKED (on object monitor) at com.jvm.visualvm.Demo4$SynAddRunalbe.run(Demo4.java:42) - waiting to lock <0x00000007173d41b8> (a com.jvm.visualvm.Demo4$User) - locked <0x00000007173d4178> (a com.jvm.visualvm.Demo4$User) at java.lang.Thread.run(Thread.java:745)"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x0000000027dd9000 nid=0x7f80 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x0000000027d80800 nid=0x3b94 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x0000000027d7c000 nid=0x4c7c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x0000000027d7b000 nid=0x8f4c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000000027d73000 nid=0x9094 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x0000000027d0a800 nid=0x647c runnable [0x00000000282ce000] 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 <0x0000000717449cd8> (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 <0x0000000717449cd8> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000027b61000 nid=0x7cf8 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000027b5f800 nid=0x94f4 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000027af1800 nid=0x9960 in Object.wait() [0x0000000027fce000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000717188ec8> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x0000000717188ec8> (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=0x0000000002deb000 nid=0xb2bc in Object.wait() [0x0000000027acf000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000717186b68> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x0000000717186b68> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)"VM Thread" os_prio=2 tid=0x0000000025be9000 nid=0xaa90 runnable"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002d0a000 nid=0x3fc0 runnable"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002d0b800 nid=0x9cf8 runnable"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002d0d000 nid=0x51c runnable"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002d0e800 nid=0x24d0 runnable"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000002d12000 nid=0x618c runnable"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000002d13000 nid=0x5550 runnable"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000002d16000 nid=0x8994 runnable"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000002d17800 nid=0x39cc runnable"VM Periodic Task Thread" os_prio=2 tid=0x0000000027dd9800 nid=0x5294 waiting on conditionJNI global references: 33Found one Java-level deadlock:============================="thread2": waiting to lock monitor 0x0000000028846468 (object 0x00000007173d4178, a com.jvm.visualvm.Demo4$User), which is held by "thread1""thread1": waiting to lock monitor 0x0000000025bf0bb8 (object 0x00000007173d41b8, a com.jvm.visualvm.Demo4$User), which is held by "thread2"Java stack information for the threads listed above:==================================================="thread2": at com.jvm.visualvm.Demo4$SynAddRunalbe.run(Demo4.java:49) - waiting to lock <0x00000007173d4178> (a com.jvm.visualvm.Demo4$User) - locked <0x00000007173d41b8> (a com.jvm.visualvm.Demo4$User) at java.lang.Thread.run(Thread.java:745)"thread1": at com.jvm.visualvm.Demo4$SynAddRunalbe.run(Demo4.java:42) - waiting to lock <0x00000007173d41b8> (a com.jvm.visualvm.Demo4$User) - locked <0x00000007173d4178> (a com.jvm.visualvm.Demo4$User) at java.lang.Thread.run(Thread.java:745)Found 1 deadlock.

从上面的堆栈信息中我们可以发现这个内容:“ Found one Java-level deadlock ”,表示程序中发现了一个死锁,后面包含跟多详细的信息,重点下面:

Java stack information for the threads listed above:==================================================="thread2": at com.jvm.visualvm.Demo4$SynAddRunalbe.run(Demo4.java:49) - waiting to lock <0x00000007173d4178> (a com.jvm.visualvm.Demo4$User) - locked <0x00000007173d41b8> (a com.jvm.visualvm.Demo4$User) at java.lang.Thread.run(Thread.java:745)"thread1": at com.jvm.visualvm.Demo4$SynAddRunalbe.run(Demo4.java:42) - waiting to lock <0x00000007173d41b8> (a com.jvm.visualvm.Demo4$User) - locked <0x00000007173d4178> (a com.jvm.visualvm.Demo4$User) at java.lang.Thread.run(Thread.java:745)

可以死锁的代码是在Demo4.java的49行和42行,此时我们就可以去优化代码,解决死锁问题。

通过jdk提供的工具jconsole排查死锁问题

jconsole:jdk提供的一个可视化的工具,方便排查程序的一些问题,如:程序内存溢出、死锁问题等等。更详细的用法见文档最后。jconsole位于jdk的bin目录中

步骤一:打开jconsole.exe

可以看到我们的程序,点击连接。

步骤二:在jconsole窗口中查看线程堆栈信息

步骤三:堆栈中查看到死锁信息

点击“检测死锁”,可以看到程序死锁信息。

上图中可以看到详细的死锁信息,和jstack中信息类似。

通过jdk提供的工具VisualVM排查死锁问题

VisualVM:jdk提供的一个非常强大的排查java程序问题的一个工具,可以监控程序的性能、查看jvm配置信息、堆快照、线程堆栈信息。算是程序优化的必备工具。

工具位于jdk的bin目录中。

步骤一:打开VisualVM.exe

步骤二:在visualvm中查看我们的目标程序

目标程序在visualvm左侧窗口中,双击即可打开。

步骤三:切换到“线程”窗口

步骤四:查看堆栈信息

在线程窗口中点击“线程Dump”按钮

可以看到产生了一个线程堆栈快照

线程堆栈快照的信息和jstack查看到的信息一样,即可发现死锁代码。

总结

程序中介绍了3中排查死锁的方法,使用到的都是jdk提供给我们的工具,这些工具对于我们优化程序来说,是非常好的,我们一定要掌握。

标签: #java惊魂