How do I see if I have a Java thread deadlock? Two methods are described below.
I. Jconsole
Jconsole is a graphical interface tool that comes with the JDK, using the tool jconsole from the JDK, which can be opened by opening cmd and typing jconsole.
Connect to the process you want to view.
Open the Threads tab, and then click Detect deadlock in the lower-left corner.
Jconsole will show us the thread that caused the deadlock in the thread, click to select to see the details:
From which we can see:
Thread Thread-1, from the state can be seen, it wants to apply [email protected] This resource, but this resource has been Thread-0 owned, so it is blocked.
Thread Thread-0, from the state can be seen, it wants to apply [email protected] This resource, but this resource has been Thread-1 owned, so it is blocked.
Thread-1 has been waiting for [email protected] resources, and thread–0 has been waiting for [email protected] resources, so the two threads are so deadlocked, resulting in deadlock.
Two. Jstack
Jstack is a command-line tool that comes with the JDK and is used primarily for thread dump analysis.
1. We first use JPS to view the Java process ID (or Linux PS command)
2. Take a look at the use of Jstack
3.jstack output thread dump information to file
Compare projects to see the difference between-L and without-L is as follows:
4. View the dump file and analyze it
One of these is at Deadthread.run (deadthread.java:37), which shows that Thread-1 has a deadlock at 37 rows in the Deadthread class, where at Deadthread.run (Deadthread.java : 21), indicating that Thread-0 is deadlocked at 21 rows of the Deadthread class. Detailed Jstack dump file analysis See: http://www.cnblogs.com/flyingeagle/articles/6853454.html.
In order to locate the cause of the deadlock, and the exact location: Thread-0 obtained the lock lock1, next expect to obtain the lock Lock2, (line 20th), but at this time Thread-1 obtained the lock Lock2, next expect to obtain the lock Lock2, (line 37th), thus a deadlock occurred.
Attached Instance Deadthread.java code:
public class Deadthread implements Runnable {public String username; public Object lock1 = new Object (); public Object Lock2 = new Object (); @Override public void Run () {//TODO auto-generated Method stub if (Username.equals ("a")) { Synchronized (lock1) {try {System.out.println ("username =" + username); System.out.println (Thread.CurrentThread (). GetName ()); Thread.Sleep (3000); } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); } synchronized (Lock2) {System.out.println ("Execute code in Lock1->lock2 order"); }}} if (Username.equals ("B")) {synchronized (Lock2) {try { System.out.println ("username =" + username); System.out.println (Thread.CurrentThread ().GetName ()); Thread.Sleep (3000); } catch (Exception e) {//Todo:handle Exception e.printstacktrace (); } synchronized (Lock1) {System.out.println ("Execute code in LOCK2->LOCK1 order"); }}}} public void Setflag (String username) {this.username = username; } public static void Main (string[] args) {Deadthread dt1 = new Deadthread (); Dt1.setflag ("a"); thread T1 = new Thread (DT1); T1.start (); try {thread.sleep (2000); } catch (Interruptedexception e) {e.printstacktrace (); } dt1.setflag ("B"); Thread t2 = new Thread (DT1); T2.start (); }}
Java Thread deadlock View analysis method