1、package com.gzrj.lesson4.deadLock;public class TestDeadLock implements Runnableprivate Object o;private Object o2;public TestDeadLock(Object o,Object o2)this.o = o;this.o2 = o2;public void run() synchronized (o) System.out.println(“线程一我已经锁定 O 对象,等待锁定 O2“);try Thread.sleep(500); catch (InterruptedExc
2、eption e) e.printStackTrace();synchronized (o2) System.out.println(“线程一我已经锁定 O2 对象,完成运行“);package com.gzrj.lesson4.deadLock;public class TestDeadLock2 implements Runnableprivate Object o;private Object o2;public TestDeadLock2(Object o,Object o2)this.o = o;this.o2 = o2;public void run() synchronized
3、(o2) System.out.println(“线程二我已经锁定 O2 对象,等待锁定 O1“);try Thread.sleep(500); catch (InterruptedException e) e.printStackTrace();synchronized (o) System.out.println(“线程二我已经锁定 O 对象,完成运行“);package com.gzrj.lesson4.deadLock;public class Test public static void main(String args) Object o = new Object();Object o2 = new Object();Thread thread = new Thread(new TestDeadLock(o,o2);Thread thread2 = new Thread(new TestDeadLock2(o,o2);thread.start();thread2.start();