public class DeadLock {public static Object Chopstick_1 = new Object();public static Object Chopstick_2 = new Object();public static void main(String[] args) {final DeadLock deadLock = new DeadLock();// 第一個線程 new Thread(new Runnable() {public void run() {try {synchronized (Chopstick_1) {System.out.println("甲得到筷子1");Thread.sleep(1000); // 當前線程睡一會,讓另外一個線程保證能得到Chopstick_2的執行權 synchronized (Chopstick_2) {System.out.println("甲得到筷子2");}}} catch (InterruptedException e) {throw new RuntimeException(e);}}}).start();// 第二個線程 new Thread(new Runnable() {public void run() {try {synchronized (Chopstick_2) {System.out.println("乙得到筷子2");Thread.sleep(1000); // 當前線程睡一會,讓另外一個線程保證能得到Chopstick_1的執行權 synchronized (Chopstick_1) {System.out.println("乙得到筷子1");}}} catch (InterruptedException e) {throw new RuntimeException(e);}}}).start();}
}
?