死锁代码
package com.zy.concurent;
public class DeadLock {
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
Thread thread1 = new Thread(new DeadLockRunalbe(obj1, obj2, 1, 2, true));
thread1.setName("thread1");
thread1.start();
Thread thread2 = new Thread(new DeadLockRunalbe(obj1, obj2, 2, 1, false));
thread2.setName("thread2");
thread2.start();
}
/**
* 线程死锁等待演示
*/
public static class DeadLockRunalbe implements Runnable {
Object object1;
Object object2;
int a, b;
boolean flag;
public DeadLockRunalbe(Object obj1, Object obj2, int a, int b, boolean flag) {
this.object1 = obj1;
this.object2 = obj2;
this.a = a;
this.b = b;
this.flag = flag;
}
@Override
public void run() {
try {
if (flag) synchronized (object1) {
Thread.sleep(100);
synchronized (object2) {
System.out.println(a + b);
}
}
else synchronized (object2) {
Thread.sleep(100);
synchronized (object1) {
System.out.println(a + b);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
通过jstack查看死锁信息:
通过jps -l查看死锁的进程ID:
jstack -l 8932查看死锁进程:
E:\project\interview\src\main\java\com\zy\concurent>jstack -l 8932
2020-03-15 16:21:50
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.192-b12 mixed mode):
"DestroyJavaVM" #14 prio=5 os_prio=0 tid=0x00000000030a3800 nid=0x1184 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"thread2" #13 prio=5 os_prio=0 tid=0x000000001e7c1000 nid=0x4390 waiting for monitor entry [0x000000002038e000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.zy.concurent.DeadLock$SynAddRunalbe.run(DeadLock.java:45)
- waiting to lock <0x000000076b0f7480> (a com.zy.concurent.DeadLock$Obj1)
- locked <0x000000076b0f9548> (a com.zy.concurent.DeadLock$Obj2)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"thread1" #12 prio=5 os_prio=0 tid=0x000000001e7c0000 nid=0x728 waiting for monitor entry [0x000000002028f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.zy.concurent.DeadLock$SynAddRunalbe.run(DeadLock.java:39)
- waiting to lock <0x000000076b0f9548> (a com.zy.concurent.DeadLock$Obj2)
- locked <0x000000076b0f7480> (a com.zy.concurent.DeadLock$Obj1)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- None
"Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001e636800 nid=0x46b8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001e640800 nid=0x4548 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001e63e000 nid=0x1a0c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001e63b000 nid=0x3b44 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001e633000 nid=0x2120 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001e602000 nid=0x3d88 runnable [0x000000001fb8e000]
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 <0x000000076b24fa98> (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 <0x000000076b24fa98> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:61)
Locked ownable synchronizers:
- None
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001e53c800 nid=0x40e4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001e592800 nid=0x46f8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000000319a800 nid=0x4d4 in Object.wait() [0x000000001f88f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076af88ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x000000076af88ed0> (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)
Locked ownable synchronizers:
- None
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001e523000 nid=0x2754 in Object.wait() [0x000000001f78f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076af86bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x000000076af86bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Locked ownable synchronizers:
- None
"VM Thread" os_prio=2 tid=0x000000001e502800 nid=0x2014 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000030b8800 nid=0x4524 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000030ba000 nid=0x4568 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000030bc000 nid=0x17c4 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000030be800 nid=0x273c runnable
"GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000030c0800 nid=0x3be0 runnable
"GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000030c1800 nid=0x338c runnable
"GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000030c5000 nid=0x514 runnable
"GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000030c6000 nid=0x3f44 runnable
"GC task thread#8 (ParallelGC)" os_prio=0 tid=0x00000000030c7000 nid=0x24b4 runnable
"GC task thread#9 (ParallelGC)" os_prio=0 tid=0x00000000030c8800 nid=0x472c runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001e660800 nid=0x44f8 waiting on condition
JNI global references: 12
Found one Java-level deadlock:
=============================
"thread2":
waiting to lock monitor 0x000000001ce43728 (object 0x000000076b0f7480, a com.zy.concurent.DeadLock$Obj1),
which is held by "thread1"
"thread1":
waiting to lock monitor 0x000000001ce42288 (object 0x000000076b0f9548, a com.zy.concurent.DeadLock$Obj2),
which is held by "thread2"
Java stack information for the threads listed above:
===================================================
"thread2":
at com.zy.concurent.DeadLock$SynAddRunalbe.run(DeadLock.java:45)
- waiting to lock <0x000000076b0f7480> (a com.zy.concurent.DeadLock$Obj1)
- locked <0x000000076b0f9548> (a com.zy.concurent.DeadLock$Obj2)
at java.lang.Thread.run(Thread.java:748)
"thread1":
at com.zy.concurent.DeadLock$SynAddRunalbe.run(DeadLock.java:39)
- waiting to lock <0x000000076b0f9548> (a com.zy.concurent.DeadLock$Obj2)
- locked <0x000000076b0f7480> (a com.zy.concurent.DeadLock$Obj1)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
可以看到:
java中几种工具的介绍:
jstack
jstack是java虚拟机自带的一种堆栈跟踪工具。
jstack用于生成java虚拟机当前时刻的线程快照。线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因。如线程间死锁、死循环、请求外部资源导致长时间等待等。线程出现停顿的时候通过jstack来查看各个线程的调用堆栈,就知道没有响应的线程到底在后台做什么事情,或者等待什么资源。
常用命令:
jstack -l pid
jstack -m pid
jps:
java提供的一个显示当前所有java进程pid命令。适合在Linux/unix平台简单查看当前java进程的一些简单情况。
常用:jps -l