package html; import java.util.concurrent.TimeUnit; /** * 模拟死锁 * * @author jis * */ public class KN { public static void main(String[] args) { Object o1 = new Object(); Object o2 = new Object(); new Thread(new Test(o1, o2)).start(); new Thread(new Test(o2, o1)).start(); } static class Test implements Runnable { Object o1; Object o2; Test(Object o1, Object o2) { this.o1 = o1; this.o2 = o2; } @Override public void run() { synchronized (o1) { System.err.println("enter p1"); try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (o2) { System.err.println("enter p2"); } } System.err.println("end"); } } }