java 多线程争抢资源死锁

多线程争抢资源死锁的原理就是,A线程正在持有锁1却想获取锁2,B线程正在持有锁2却要获取锁1

代码如下:

public class Main {
static ReentrantLock lock1 = new ReentrantLock();
static ReentrantLock lock2 = new ReentrantLock(); static class Test1 implements Runnable {
@Override
public void run() {
lock1.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("尝试获取lock2");
lock2.lock();
lock2.unlock();
lock1.unlock();
}
} static class Test2 implements Runnable {
@Override
public void run() {
lock2.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("尝试获取lock1");
lock1.lock();
lock1.unlock();
lock2.unlock();
}
} public static void main(String[] args) {
new Thread(new Test1()).start();
new Thread(new Test2()).start();

排查方法:

而我们通过 jps -l 命令找到java对应的进程id,根据id执行命令:jstack 12678 就会获取到栈的信息

D:\s_p\com.server.session>jps -l
6928 org.jetbrains.jps.cmdline.Launcher
10228 sun.tools.jps.Jps
12724 com.server.session.Main
10744 org.jetbrains.jps.cmdline.Launcher
13160 org.apache.catalina.startup.Bootstrap
8392 org.jetbrains.idea.maven.server.RemoteMavenServer
2812
4828 com.viewhigh.bi.parent.BiApplication

D:\s_p\com.server.session>jstack 12724
2019-04-29 16:38:10
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.31-b07 mixed mode):

"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000002db5800 nid=0x3044 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Thread-1" #12 prio=5 os_prio=0 tid=0x000000001a39e000 nid=0x2648 waiting on condition [0x000000001b4be000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d5e9f570> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at com.server.session.Main$Test2.run(Main.java:53)
at java.lang.Thread.run(Thread.java:745)

"Thread-0" #11 prio=5 os_prio=0 tid=0x000000001a361800 nid=0x1190 waiting on condition [0x000000001b3be000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d5e9f5a0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at com.server.session.Main$Test1.run(Main.java:37)
at java.lang.Thread.run(Thread.java:745)

"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x0000000019c23800 nid=0x3cb0 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x0000000019c17000 nid=0x11e0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x0000000019bc5800 nid=0x1c58 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000000019bc5000 nid=0x19b4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x0000000019b8e800 nid=0x3e5c runnable [0x0000000019dee000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
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 <0x00000000d5ef0030> (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 <0x00000000d5ef0030> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:63)

"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000018f33800 nid=0x1244 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000017bbc000 nid=0x17a0 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000002eab800 nid=0x41fc in Object.wait() [0x0000000018eee000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d57062f8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:142)
- locked <0x00000000d57062f8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:158)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000017b68800 nid=0x217c in Object.wait() [0x0000000018def000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5705d68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
- locked <0x00000000d5705d68> (a java.lang.ref.Reference$Lock)

"VM Thread" os_prio=2 tid=0x0000000017b65800 nid=0x1bfc runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002dd3800 nid=0xba8 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002dd5000 nid=0xb98 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000002dd6800 nid=0x3d10 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000002dd8000 nid=0x1290 runnable

"VM Periodic Task Thread" os_prio=2 tid=0x0000000019ca8800 nid=0x31c8 waiting on condition

JNI global references: 32

Found one Java-level deadlock:
=============================
"Thread-1":
waiting for ownable synchronizer 0x00000000d5e9f570, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
which is held by "Thread-0"
"Thread-0":
waiting for ownable synchronizer 0x00000000d5e9f5a0, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d5e9f570> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at com.server.session.Main$Test2.run(Main.java:53)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d5e9f5a0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at com.server.session.Main$Test1.run(Main.java:37)
at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

上一篇:【计算机网络】WebSocket实现原理分析


下一篇:Java多线程系列——线程阻塞工具类LockSupport