| ||||
|
Code - Class EDU.oswego.cs.dl.util.concurrent.LayeredSync1 /* 2 File: LayeredSync.java 3 4 Originally written by Doug Lea and released into the public domain. 5 This may be used for any purposes whatsoever without acknowledgment. 6 Thanks for the assistance and support of Sun Microsystems Labs, 7 and everyone contributing, testing, and using this code. 8 9 History: 10 Date Who What 11 1Aug1998 dl Create public version 12 */ 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 /** 17 * A class that can be used to compose Syncs. 18 * A LayeredSync object manages two other Sync objects, 19 * <em>outer</em> and <em>inner</em>. The acquire operation 20 * invokes <em>outer</em>.acquire() followed by <em>inner</em>.acquire(), 21 * but backing out of outer (via release) upon an exception in inner. 22 * The other methods work similarly. 23 * <p> 24 * LayeredSyncs can be used to compose arbitrary chains 25 * by arranging that either of the managed Syncs be another 26 * LayeredSync. 27 * 28 * <p>[<a HREF="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] 29 **/ 30 31 32 public class LayeredSync implements Sync { 33 34 protected final Sync outer_; 35 protected final Sync inner_; 36 37 /** 38 * Create a LayeredSync managing the given outer and inner Sync 39 * objects 40 **/ 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 |
|||
Java API By Example, From Geeks To Geeks. |
Conditions of Use |
About Us
© 2002 - 2005, KickJava.com, or its affiliates
|