00001 public class NestedMonitorDeadlock { 00002 public static void main(String[] args) { 00003 Wrapper w = new Wrapper(new Pair()); 00004 (new Thread1(w)).start(); 00005 (new Thread2(w)).start(); 00006 } 00007 } 00008 00009 class Pair { 00010 private int hi = 0; 00011 private int lo = 0; 00012 00013 public synchronized void inclo() { 00014 while (lo <= hi) 00015 try { wait(); } catch ( InterruptedException ex) {} 00016 lo++; 00017 } 00018 00019 public synchronized void inchi() { 00020 hi++; 00021 notify(); 00022 } 00023 } 00024 00025 class Wrapper { 00026 private Pair p; 00027 Wrapper(Pair x) {p = x;} 00028 public synchronized void inclo() { p.inclo(); } 00029 public synchronized void inchi() { p.inchi(); } 00030 } 00031 00032 class Thread1 extends Thread { 00033 private Wrapper w; 00034 Thread1(Wrapper x) {w = x;} 00035 public void run() { 00036 w.inclo(); 00037 } 00038 } 00039 00040 class Thread2 extends Thread { 00041 private Wrapper w; 00042 Thread2(Wrapper x) {w = x;} 00043 public void run() { 00044 w.inchi(); 00045 } 00046 } 00047 00048