So how do we know it must be a deadlock? There are two ways.
1> use the JDK to our tool jconsole, which can be opened by opening cmd and typing jconsole.
1) Connect to the process you want to view.
2) Open the Threads tab, then click "Detect Deadlock" in the lower left corner
3) Jconsole will show us the thread that caused the deadlock in this 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 for paper 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 for pen this resource, but this resource has been Thread-1 owned, so it is blocked.
Thread-1 has been waiting for paper resources, and thread--has been waiting for a pen resource, so the two threads are so deadlocked, resulting in a deadlock.
2> Direct use of commands from the JVM
1) first through the JPS command to see the Java process needs to see the vmid, we want to see the process Testdeadlock vmid number is 7412;
2) then use Jstack to view the stack situation in the process, enter Jstack-l 7412 in cmd, and move to the bottom of the output information to get:
At this point, I believe everyone will look at it, specifically do not say, according to the output, find the code of the problem, start debugging solution can be.
How Java looks at deadlocks