废话不多说,talk is cheap show me the code: public static void main(String[] args) { String s1 = "1"; String s2 = "2"; new Thread(()->{ synchronized (s1){ System.out.println(s1); try { Thread.sleep(1000);//确保线程2有足够时间对s2对象加锁。 } catch (InterruptedException e) { e.printStackTrace(); } synchronized (s2){ System.out.println(s2); } } }).start(); new Thread(()->{ synchronized (s2){ System.out.println(s2); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (s1){ System.out.println(s1); } } }).start(); }
08-15
08-15
08-15