Here is a sample code for the deadlock condition
Package Com.qust.demo.money;class A {public synchronized void foo (b b) {System.out.println (Thread.CurrentThread (). GetName () + "Foo to enter a"), try {thread.sleep ($);} catch (Interruptedexception ex) {ex.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "attempted to invoke the last of B"); B.last (); Public synchronized Void Last () {System.out.println (last () of "a");}} Class B {public synchronized void bar (a a) {System.out.println (Thread.CurrentThread (). GetName () + "bar entering B"); try {Thread . Sleep (200);} catch (Interruptedexception ex) {ex.printstacktrace ();} System.out.println (Thread.CurrentThread (). GetName () + "attempted to invoke the last of a"); A.last (); Public synchronized Void Last () {System.out.println (last () of "B");}} public class DeadLock implements Runnable {A A = new A (); b b = new B ();p ublic void init () {Thread.CurrentThread (). SetName ("main thread"); System.out.println ("Enter the main thread"); A.foo (b);} public void Run () {Thread.CurrentThread (). SetName ("sub-thread"); SYSTEM.OUT.PRINTLN ("Incoming sub-thread"); B.bar (a);} public static void Main (string[] args) {DeadLock dl = new DeadLock (), New Thread (DL). Start ();d l.init ();}}
Here is the result of the run
Enter the main thread into the vice-threading thread to enter a's Foo secondary thread into B's Bar secondary thread trying to call A's last main thread trying to invoke the last of B
We see that, under normal circumstances, the last () and B of "a" should be printed out, but because of the thread deadlock, the program has been waiting for execution and failed to perform properly.
In the above code, why is there a deadlock situation? Let's take a brief look at the analysis.
First, although the start () of the secondary thread is before the main thread's init (), it takes a while for multithreading to open, so we can see that the init () method of the main thread executes before, and then A.foo () is called in Init (). The methods in Class A and Class B are synchronous, so both the object of a and the object B are synchronous locks. Before entering a foo (), the thread will lock a and then the thread sleeps 200ms, when the secondary thread calls B's Bar (), the same will lock the B, and then sleep 200ms. At this time, the main thread wakes up, trying to invoke the bar method of B, because it is a synchronous method, so we need to lock the B, but this time B has been locked by the secondary thread, so the main thread has been in a blocking state. When the secondary thread wakes up, trying to call a synchronous method, also need a lock, but at this time the main thread holds a lock, and is in a blocking state, so the secondary thread can not be executed down, we are waiting for the other party to release the lock, so there is the deadlock we said above situation.
If we remove the last () synchronized of A and B , then the program does not need to lock the object before calling last (), and there will be no deadlock.
Because in the work has not encountered this situation, so can only take this instance program to explain.
An example of a deadlock condition in Java Multi-threading