"http://www.itsoku.com/archives"
>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
set
Name(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 -l
5824 com.jvm.visualvm.Demo4
步骤二:使用jstack查看线程堆栈信息
jstack:jdk提供的一个工具,可以查看java进程中线程堆栈信息。更详细的用法见文档最后。
C:\Users\Think>jstack 5824
2019-06-04 15:34:37
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):
"DestroyJavaVM"
java.lang.Thread.State: RUNNABLE
"thread2"
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"
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"
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread3"
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread2"
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1"
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0"
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break"
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"
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher"
java.lang.Thread.State: RUNNABLE
"Finalizer"
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"
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 condition
JNI global references: 33
Found 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提供给我们的工具,这些工具对于我们优化程序来说,是非常好的,我们一定要掌握。
王也518