1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 30 31 32 public class LayeredSync implements Sync { 33 34 protected final Sync outer_; 35 protected final Sync inner_; 36 37 41 42 public LayeredSync(Sync outer, Sync inner) { 43 outer_ = outer; 44 inner_ = inner; 45 } 46 47 public void acquire() throws InterruptedException { 48 outer_.acquire(); 49 try { 50 inner_.acquire(); 51 } 52 catch (InterruptedException ex) { 53 outer_.release(); 54 throw ex; 55 } 56 } 57 58 public boolean attempt(long msecs) throws InterruptedException { 59 60 long start = (msecs <= 0)? 0 : System.currentTimeMillis(); 61 long waitTime = msecs; 62 63 if (outer_.attempt(waitTime)) { 64 try { 65 if (msecs > 0) 66 waitTime = msecs - (System.currentTimeMillis() - start); 67 if (inner_.attempt(waitTime)) 68 return true; 69 else { 70 outer_.release(); 71 return false; 72 } 73 } 74 catch (InterruptedException ex) { 75 outer_.release(); 76 throw ex; 77 } 78 } 79 else 80 return false; 81 } 82 83 public void release() { 84 inner_.release(); 85 outer_.release(); 86 } 87 88 } 89 90 91 | Popular Tags |