1 package 多线程; 2 class A{ 3 public synchronized void say(B b){ 4 System.out.println("A说:你把你的本给我,我把我的笔给你!"); 5 b.get(); 6 } 7 public synchronized void get(){ 8 System.out.println("A说:我得到了本,给你笔。"); 9 } 10 } 11 class B{ 12 public synchronized void say(A a){ 13 System.out.println("B说:你把你的笔给我,我把我的本给你!"); 14 a.get(); 15 } 16 public synchronized void get(){ 17 System.out.println("B说:我行到了笔,给你本。"); 18 } 19 }//以上任何一个synchronized去掉就可解除死锁 20 public class TestDeadLock implements Runnable{ 21 private static A a=new A(); 22 private static B b=new B(); 23 public static void main(String[] args) { 24 new TestDeadLock(); 25 } 26 public TestDeadLock(){ 27 new Thread(this).start(); 28 b.say(a); 29 } 30 @Override 31 public void run() { 32 a.say(b); 33 } 34 }