1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 46 47 public class Latch implements Sync { 48 protected boolean latched_ = false; 49 50 59 60 public void acquire() throws InterruptedException { 61 if (Thread.interrupted()) throw new InterruptedException (); 62 synchronized(this) { 63 while (!latched_) 64 wait(); 65 } 66 } 67 68 public boolean attempt(long msecs) throws InterruptedException { 69 if (Thread.interrupted()) throw new InterruptedException (); 70 synchronized(this) { 71 if (latched_) 72 return true; 73 else if (msecs <= 0) 74 return false; 75 else { 76 long waitTime = msecs; 77 long start = System.currentTimeMillis(); 78 for (;;) { 79 wait(waitTime); 80 if (latched_) 81 return true; 82 else { 83 waitTime = msecs - (System.currentTimeMillis() - start); 84 if (waitTime <= 0) 85 return false; 86 } 87 } 88 } 89 } 90 } 91 92 93 public synchronized void release() { 94 latched_ = true; 95 notifyAll(); 96 } 97 98 } 99 100 | Popular Tags |