Package Com.simon.study;/** thread deadlock A thread must have two objects at the same time for the next operation; * @author: Simon * @date: July 29, 2017 pm 9:48:43*/ Public classDeadLock implements runnable{StaticObject o1=NewObject (), o2=NewObject (); Public intflag=1; Public voidrun () {//TODO auto-generated Method StubSystem. out. println ("flag***********"+flag); if(flag==1) {synchronized (O1) {Try{Thread.Sleep ( -); } Catch(Exception e) {//Todo:handle Exception} synchronized (O2) {System. out. println ("here thread 1 gets all the thread permissions"); } } } if(flag==0) {synchronized (O2) {Try{Thread.Sleep ( -); } Catch(Exception e) {//Todo:handle Exception} synchronized (O1) {System. out. println ("here thread 2 gets all the thread permissions"); } } } } Public Static voidMain (string[] args) {DeadLock deadLock1=NewDeadLock (); DeadLock DeadLock2=NewDeadLock (); Deadlock1.flag=1; Deadlock2.flag=0; Thread R1=NewThread (DEADLOCK1); Thread R2=NewThread (DEADLOCK2); R1.start (); R2.start (); } }
Write a Java deadlock demo