Deadlock AB-BA problem

AB-BA is often the biggest culprits causing multi-threaded deadlock, manifested in the thread I have to pick up the lock A and lock B, however, it has been found in another thread II get away when the lock B, but to get the thread II B then lock the lock needs to A, then waiting for another two threads in a deadlock into

 1 public class Main{
 2      public static void main(String[] args){
 3         System.out.println("演示开始");
 4            new Thread(()->{
 5              A.a();
 6            }).start();
 7            new Thread(()->{
 8              B.b();
 9            }).start();
10      }
11 
12      static class A{
13         public static synchronized void a(){
14             try{
 15                  the Thread.sleep (3000 );    
 16              } the catch (Exception E) {
 . 17                  e.printStackTrace ();
 18 is              }
 . 19              
20 is  
21 is              System.out.println ( "I got a lock, the lock b coming to the bowl ! " );
 22 is  
23 is              Bb ();
 24          }
 25       }
 26 is  
27       static  class B {
 28          public  static  the synchronized  void b () {
 29              System.out.println (" I got a b lock, a lock approaching bowl ! come " );
 30             try{
31                 Thread.sleep(3000);    
32             }catch(Exception e){
33                 e.printStackTrace();
34             }
35             A.a();
36         }
37      }
38  }

 

Guess you like

Origin www.cnblogs.com/shaozhen/p/11127126.html