多线程环境下,相互或循环持有对方需要的资源,从而导致死锁。
解决方案:
事前:避免相互持有对方的锁,或者循环持有下一个所需要的锁的情况。尽量将某个资源限制在线程内部,避免资源争抢。
事中:找出死锁并强制其中一个退出,即可打破死锁状态
事后:优化代码,如:优化锁顺序,粗化锁粒度等。
下面,手写一个最简单的死锁。
package com.zm.demo.core.concurrent;
import static java.lang.System.out;
/**
* Created by Administrator on 2020/2/29.
*/
public class DeadLock {
public static void main(String[]args){
String source1= "source1";
String source2= "source2";
Thread t1 = new Thread(()->{
//持有source1
synchronized (source1){
out.println("t1 lock " + source1);
//申请source2
synchronized (source2){
out.println("t1 lock " + source2);
}
}
});
Thread t2 = new Thread(()->{
//持有source2
synchronized (source2){
out.println(" t2 lock " + source2);
//申请source1
synchronized (source1){
out.println("t2 lock " + source1);
}
}
});
t1.start();
t2.start();
}
}
使用jstack命令查看进程栈信息,可以找出死锁状态的线程。
#找出当前环境下进程信息
D:\git\zm\demo>jps
10704 AppMain
6864 Launcher
9624
5100 Jps
9212 RemoteMavenServer
#打印进程栈信息
D:\git\zm\demo>jstack 10704
2020-02-29 19:13:21
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.91-b14 mixed mode):
"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x00000000021ff000 nid=0x418 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-1" #12 prio=5 os_prio=0 tid=0x00000000098dd000 nid=0x628 waiting for monitor entry [0x000000000b23f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.zm.demo.core.concurrent.DeadLock.lambda$main$1(DeadLock.java:27)
- waiting to lock <0x00000000d5cf5838> (a java.lang.String)
- locked <0x00000000d5cf5870> (a java.lang.String)
at com.zm.demo.core.concurrent.DeadLock$$Lambda$2/1023892928.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Thread-0" #11 prio=5 os_prio=0 tid=0x00000000098dc000 nid=0x1528 waiting for monitor entry [0x0000000009a4f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.zm.demo.core.concurrent.DeadLock.lambda$main$0(DeadLock.java:18)
- waiting to lock <0x00000000d5cf5870> (a java.lang.String)
- locked <0x00000000d5cf5838> (a java.lang.String)
at com.zm.demo.core.concurrent.DeadLock$$Lambda$1/1831932724.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Monitor Ctrl-Break" #10 daemon prio=5 os_prio=0 tid=0x000000000965d000 nid=0x280c runnable [0x000000000a6cf000]
java.lang.Thread.State: RUNNABLE
at java.net.DualStackPlainSocketImpl.accept0(Native Method)
at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
- locked <0x00000000d5df5730> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x00000000095c6000 nid=0x2440 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x0000000009551800 nid=0x14b4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x0000000009550800 nid=0x2a38 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x00000000081be800 nid=0x27c0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000000953b000 nid=0x1ba4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000000953a800 nid=0x15cc runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000008199800 nid=0x2460 in Object.wait() [0x000000000952e000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5c08ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000d5c08ee0> (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=0x0000000008152800 nid=0x233c in Object.wait() [0x000000000932f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5c06b50> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000d5c06b50> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x000000000814b000 nid=0x2b4c runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x000000000224d000 nid=0x2a34 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x000000000224e800 nid=0x21f4 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002250000 nid=0x1604 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002251800 nid=0x1aec runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000000960a800 nid=0x1a78 waiting on condition
JNI global references: 317
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x0000000008156898 (object 0x00000000d5cf5838, a java.lang.String),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x0000000008159338 (object 0x00000000d5cf5870, a java.lang.String),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.zm.demo.core.concurrent.DeadLock.lambda$main$1(DeadLock.java:27)
- waiting to lock <0x00000000d5cf5838> (a java.lang.String)
- locked <0x00000000d5cf5870> (a java.lang.String)
at com.zm.demo.core.concurrent.DeadLock$$Lambda$2/1023892928.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at com.zm.demo.core.concurrent.DeadLock.lambda$main$0(DeadLock.java:18)
- waiting to lock <0x00000000d5cf5870> (a java.lang.String)
- locked <0x00000000d5cf5838> (a java.lang.String)
at com.zm.demo.core.concurrent.DeadLock$$Lambda$1/1831932724.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.
最后几行,很明显的,直接打印出死锁信息了。而且直接打印出死锁的代码位置,我们可以很方便的找到代码并进行优化。
Thread-0持有0x00000000d5cf5838,等待0x00000000d5cf5870
同时
Thread-1持有0x00000000d5cf5870,等待0x00000000d5cf5838
因此,产生死锁。