Test thread deadlock, run the following code:
public class Test1 {
private static Object O1 = new Object ();
private static Object O2 = new Object ();
public static void Main (string[] args) {for
(int i = 0;i<100;i++) {
new Thread (new Runnable () {
@Override
public void Run () {
synchronized (O1) {
synchronized (O2) {
System.out.println ("AM O1");
}
}
}). Start ();
New Thread (New Runnable () {
@Override public
void Run () {
synchronized (O2) {
synchronized (O1) { C19/>system.out.println ("Am O2"),}}}
). Start ();}}
Code thread 1 first to apply for OBJ1, and then apply for OBJ2, threads 2 first to apply for OBJ2, and then apply for obj1. If there are more executions, a deadlock will occur, and we are still looking at the thread's monitoring console:
There are jvisualvm.exe tools in the JDK C:\Program files\java\jdk1.8.0_101\bin directory
Run the above code to view the thread, as shown in the following illustration:
Select a thread to view the dump, you can view the specific cause of the deadlock, you can use this tool to view those threads in the program deadlock, so as to better troubleshoot errors.