Examples of deadlocks
Package Day11_second;public class Deathlock {public static void main (string[] args) {//TODO auto-generated method Stubdea ThLocked1 DL1 = new DeathLocked1 ();D eathLocked2 DL2 = new DeathLocked2 (); thread T1 = new Thread (DL1); Thread t2 = new Thread (DL2); T1.start (); T2.start ();}} Class Res {public static object obj1 = new Object ();p ublic static Object obj2 = new Object ();} Class DeathLocked1 implements runnable{@Overridepublic void Run () {//TODO auto-generated method Stubfun ();} private void Fun () {//TODO auto-generated method stubsynchronized (res.obj1) {try {thread.sleep (+);} catch (Interrupte Dexception e) {e.printstacktrace ();} Synchronized (RES.OBJ2) {System.out.println ("Call Obj2 in Obj1");}}} Class DeathLocked2 implements runnable{@Overridepublic void Run () {//TODO auto-generated method Stubfun ();} private void Fun () {//TODO auto-generated method stubsynchronized (res.obj2) {try {thread.sleep (+);} catch (Interrupte Dexception e) {e.printstacktrace ();} Synchronized (res.obj1) {System.out. println ("Call obj1 in Obj2");}}}
After starting the thread T1, execute the T1 fun method, occupy the O1 resource, and then T1 hibernate to make sure that T2 can be executed. T2 executes the fun () method, occupying O2 resources. This is the fourth necessary condition for a deadlock. That is, the thread T1 occupies the necessary resources of T2, T2 occupies the necessary resources of T1, both sides do not release, that is, the formation of deadlock.
http://my.oschina.net/u/1469592/blog/208374
Examples of deadlocks