During the lock object, a problem is a deadlock that both parties are waiting to release resources
Example:
Public classTest { Public Static voidMain (string[] args)throwsException {MyThread MT1=NewMyThread (); MyThread mt2=NewMyThread (); Mt1.flag= 1; Mt2.flag= 2; Thread T1=NewThread (MT1); Thread T2=NewThread (MT2); T1.start (); T2.start (); }}classMyThreadImplementsRunnable {StaticObject Ob1 =NewObject ();//attention is static . StaticObject OB2 =NewObject ();//attention is static . intFlag = 0; Public voidrun () {if(Flag = = 1) {System.out.println ("Flag = 1"); synchronized(ob1) {Try{Thread.Sleep (1000); } Catch(interruptedexception e) {System.out.println ("Sleep Interrupt"); } synchronized(OB2) {System.out.println ("1"); } } } if(Flag = = 2) {System.out.println ("Flag = 2"); synchronized(OB2) {Try{Thread.Sleep (1000); } Catch(interruptedexception e) {System.out.println ("Sleep Interrupt"); } synchronized(OB1) {System.out.println ("2"); } } } }}
Java Learning Note deadlock