注意: 2个锁是调用关系的, 不是并列关系
public class DeadLock {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final Object resource1 = "res1";
final Object resource2 = "res2";
Thread t1 = new Thread() {
public void run() {
synchronized(resource1) {
System.out.println("Thread-1 :locked resource1");
// try {
// Thread.sleep(50);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
//
synchronized(resource2) {
System.out.println("Thread-1 :locked resource2");
}
}
}
};
Thread t2 = new Thread() {
public void run() {
synchronized(resource2) {
System.out.println("Thread-2 :locked resource2");
// try {
// Thread.sleep(50);
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// }
synchronized(resource1) {
System.out.println("Thread-2 :locked resource1");
}
}
}
};
t1.start();
t2.start();
}
}