package project; public class Main implements Runnable{ static Object s1 = new Object(),s2 = new Object(); public void run(){ if(Thread.currentThread().getName().equals("t1")){ synchronized (s1) { System.out.println("th1 locked s1"); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } synchronized (s2) { System.out.println("th1 locked s2"); } } }else { synchronized (s2) { System.out.println("th1 locked s2"); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } synchronized (s1) { System.out.println("th1 locked s1"); } } } } public static void main(String[] args) { Thread t1 = new Thread(new Main(),"t1"); Thread t2 = new Thread(new Main(),"t2"); t1.start(); t2.start(); } }