Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How java locates deadlocks

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to locate deadlocks in java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Deadlock

Deadlock: refers to two or more processes in the implementation process, due to competition for resources caused by a mutual waiting phenomenon, if there is no external force, they will not be able to move forward.

The cause of deadlock

Deadlocks occur due to resource competition, which is caused by the following reasons:

The system resources are insufficient, if the system resources are sufficient, the possibility of deadlock is very low.

The process (thread) runs in the wrong order.

Improper allocation of resources, etc.

Conditions under which deadlock occurs

Four necessary conditions for the occurrence of a deadlock:

Mutex: a resource can only be used by one process at a time.

Possession and wait: when a process is blocked by a request for resources, it holds on to the resources it has acquired.

Do not forcibly occupy: the resources that have been acquired by the process (thread) cannot be forcibly deprived until they are used up.

Loop wait condition: a loop wait resource relationship is formed between several processes (threads).

These four conditions are the necessary conditions of deadlock, as long as deadlock occurs in the system, these conditions must be established, and as long as one of the above conditions is not met, deadlock will not occur.

1: locate deadlock information through jstack

1.1: write deadlock code

Lock lock1 = new ReentrantLock (); Lock lock2 = new ReentrantLock (); ExecutorService exectuorService = Executors.newFixedThreadPool (2); exectuorService.submit (()-> {lock1.lock (); try {Thread.sleep (1000);} catch (Exception e) {} try finally {lock1.unlock (); lock2.unlock ();}}); exectuorService.submit (()-> {lock2.lock ()) Try {Thread.sleep (1000);} catch (Exception e) {} try {} finally {lock1.unlock (); lock2.unlock ();}}); 1.2: view the pid of deadlock threads

Jps view deadlocked thread pid

Use jstack-l pid to view deadlock information

By printing the information, we can find out where the deadlock code is located.

"DestroyJavaVM" # 13 prio=5 os_prio=31 tid=0x00007f9a1d8fe800 nid=0xd03 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:-None "pool-1-thread-2" # 12 prio=5 os_prio=31 tid=0x00007f9a1d8fe000 nid=0xa703 waiting on condition [0x000070000ff8e000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park (Native Method)-parking to wait for (a java.util.concurrent.locks.ReentrantLock$NonfairSync) at java.util.concurrent.locks.LockSupport.park (LockSupport.java 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.coco.util.SlideTimeUnit.lambda$main$1 (SlideTimeUnit.java:63) at com.coco. Util.SlideTimeUnit$$Lambda$2/565760380.run (Unknown Source) at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511) at java.util.concurrent.FutureTask.run (FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624) at java.lang.Thread.run (Thread.java:748) Locked ownable synchronizers:-(a java.util.concurrent.locks.ReentrantLock$NonfairSync)-(a java .util.concurrent.ThreadPoolExecutor $Worker) "pool-1-thread-1" # 11 prio=5 os_prio=31 tid=0x00007f9a2082c800 nid=0xa803 waiting on condition [0x000070000fe8b000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park (Native Method)-parking to wait for (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.coco.util.SlideTimeUnit.lambda$main$0 (SlideTimeUnit.java:49) at com.coco.util.SlideTimeUnit$$Lambda$1/596512129.run (Unknown Source) at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511) at Java.util.concurrent.FutureTask.run (FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624) at java.lang.Thread.run (Thread.java:748) Locked ownable synchronizers:-(a java.util.concurrent.locks.ReentrantLock$NonfairSync)-(a java.util.concurrent.ThreadPoolExecutor$Worker) "Service Thread" # 10 daemon prio=9 os_prio=31 tid=0x00007f9a2082c000 nid=0x4103 runnable [0x0000000000000000] java.lang .Thread.State: RUNNABLE Locked ownable synchronizers:-None "C1 CompilerThread3" # 9 daemon prio=9 os_prio=31 tid=0x00007f9a1e021800 nid=0x3f03 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE2: locate deadlocks through the Arthas tool

2.1: download the jar of Arthas, and then run

With a thread-b, you can view the deadlock information.

[arthas@4182] $thread- b "pool-1-thread-2 Id=12 WAITING on java.util.concurrent.locks.ReentrantLock$NonfairSync@2cb8a9a3 owned by" pool-1-thread-1 "Id=11 at sun.misc.Unsafe.park (Native Method)-waiting on java.util.concurrent.locks.ReentrantLock$NonfairSync@2cb8a9a3 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.coco.util.SlideTimeUnit.lambda$main$1 (SlideTimeUnit.java:63) at com.coco.util.SlideTimeUnit$$Lambda$2/565760380.run (Unknown Source) at java .util.concurrent.Executors $RunnableAdapter.call (Executors.java:511) at java.util.concurrent.FutureTask.run (FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624) at java.lang.Thread.run (Thread.java:748) Number of locked synchronizers = 2-java.util.concurrent.ThreadPoolExecutor$Worker@6433a2-java.util.concurrent.locks.ReentrantLock$NonfairSync@3a855d13

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report