Source: micro-channel public number: ape world
1. deadlock concepts:
Java is multithreaded case, two or more processes in the implementation process, due to the competition for resources or A blocking phenomenon caused due communicate with each other, without external force, they speak not promote it. At this time, say the system is in deadlock state or system to produce a deadlock, these will never become deadlocked process in the process of waiting for each other.
2. When the deadlock condition:
(1) must be two or more processes (threads)
(2) There must compete for resources
3. If the investigation and the code appears in a deadlock?
Deadlock Code:
public class JStackDemo { public static void main(String[] args) { Thread t1 = new Thread(new DeadLockTest(true)); Thread t2 = new Thread(new DeadLockTest(false)); t1.setName("thread-test-1"); t2.setName("thread-test-2"); t1.start(); t2.start(); } } class DeadLockTest implements Runnable { public booleanflag; // control thread DeadLockTest ( Boolean flag) { the this .flag = flag; } @Override public void RUN () { // If the flag is true then the calling thread t1 IF (flag) { the while ( true ) { the synchronized (Demo.o1) { System.out.println ( "O1" + Thread.currentThread () getName ().); the synchronized (Demo.o2) { System.out.println ( "O2" + Thread.currentThread().getName()); } } } } else { // 如果flag的值为false则调用t2线程 while (true) { synchronized (Demo.o2) { System.out.println("o2" + Thread.currentThread().getName()); synchronized (Demo.o1) { System.out.println("o2" + Thread.currentThread().getName()); } } } } } } class Demo { static Object o1 = new Object(); static Object o2 = new Object(); }
Method a: Use jps + jstack
1. In the windows command window, use the jps -l pid to view the current java process, it is easy to distinguish the process of their own development programs through the package path.
2. Use jstack -l 908 error message if it appears, indicating that the thread deadlock
Method Two: Using jconsole
In the Open JConsole window, JConsole is a graphical monitoring tools. (I have not tried)
1. In the windows command window, output JConsole
2. Select the tab page thread to view the thread state
Method three: Use Java Visual VM
Jvisualvm open in window, jvisualvm is a graphical monitoring tool!
Download: https://visualvm.github.io
1. In the windows command window, output jvisualvm, Java VisualVM window will pop up
2. Click the program you want to view threads, choose "thread" tab page, found that "surveillance" of the color red thread, is the deadlock. Click on the right "Thread Dump" can view specific error messages.