I think of the deadlock today, I tried to write a.
Package com.ljl.thread;
public class Deallock implements Runnable {private int flag = 1;
This object must be static, synchronization must be shared by multiple threads static Object Resource1 = new Object (), Resource2 = new Object ();
public void Run () {if (flag = 1) {synchronized (Resource1) {System.out.println ("Resource1");
try {thread.sleep (1000);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} synchronized (Resource2) {System.out.println ("Resource2");
}} if (flag = 0) {synchronized (RESOURCE2) {System.out.println ("Resource2");
try {thread.sleep (1000);
catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();
} synchronized (Resource1) {System.out.println ("Resource1");
}}} public static void Main (string[] args) {Deallock lock1 = new Deallock ();
Deallock Lock2 = new Deallock ();
Lock1.flag = 1; Lock2.Flag = 0;
Thread ll1 = new Thread (LOCK1);
Thread ll2 = new Thread (LOCK2);
Ll1.start ();
Ll2.start ();
}
}