public class Deadlock {
public static main(String[] args) {
final Object r1 = "r1";
final Object r2 = "r2";
Thread t1 = new Thread() {
public void run() {
synchronized(r1) {
System.out.println("T1 : locked r1");
try {Thread.sleep(150);}
catch(InterruptedException e) {}
synchronized(r2) {
System.out.println("T1 : locked r2");
}
}
}
};
Thread t2 = new Thread() {
public void run() {
synchronized(r2) {
System.out.println("T2 : locked r2");
try {Thread.sleep(50);}
catch(InterruptedException e) {}
synchronized(r1) {
System.out.println("T2 : locked r1");
}
}
}
};
t1.start();
t2.start();
}
}
結果輸出:(程式無法停止執行)
T1 : locked r1
T2 : locked r2
0 意見 :
張貼留言