Two threads wait for each other to release the Sync Listener, and the deadlock will occur when the other person's results are waiting to be executed.
Male to female said: You marry me first, I buy you a house again, female to Male said: you buy a house for me first, I marry you again.
Multiple threads Lock the same listener at the same time .
Avoid deadlocks in development.
Examples of deadlocks:
Public classDeadLock { Public Static voidMain (string[] args) {Resource R1=NewResource (); Resource R2=NewResource (); //each thread has a r1,r2 two objectsThread MYTH1 =NewMyThread1 (R1, r2); Thread MYTH2=NewMyThread2 (R1, r2); Myth1.start (); Myth2.start (); }}classResource {Private inti;}classMyThread1extendsThread {PrivateResource R1, R2; PublicMyThread1 (Resource R1, Resource r2) { This. R1 =R1; This. r2 =R2; } @Override Public voidrun () { while(true) { //get the lock of R1 first, and then get the lock of R2 synchronized(R1) {System.out.println ("Line Line 1 gets the R1 lock."); synchronized(R2) {System.out.println ("Line Line 1 gets the R2 lock."); } } } }}classMyThread2extendsThread {PrivateResource R1, R2; PublicMyThread2 (Resource R1, Resource r2) { This. R1 =R1; This. r2 =R2; } @Override Public voidrun () { while(true) { //get the lock of R2 first, and then get the lock of R1 synchronized(R2) {System.out.println ("Line Line 2 gets the R2 lock."); synchronized(R1) {System.out.println ("Line Line 2 gets the R1 lock."); } } } }}
The deadlock of the JDK thread