1 13 14 package EDU.oswego.cs.dl.util.concurrent; 15 16 54 55 public class CountDown implements Sync { 56 protected final int initialCount_; 57 protected int count_; 58 59 60 public CountDown(int count) { count_ = initialCount_ = count; } 61 62 63 68 public void acquire() throws InterruptedException { 69 if (Thread.interrupted()) throw new InterruptedException (); 70 synchronized(this) { 71 while (count_ > 0) 72 wait(); 73 } 74 } 75 76 77 public boolean attempt(long msecs) throws InterruptedException { 78 if (Thread.interrupted()) throw new InterruptedException (); 79 synchronized(this) { 80 if (count_ <= 0) 81 return true; 82 else if (msecs <= 0) 83 return false; 84 else { 85 long waitTime = msecs; 86 long start = System.currentTimeMillis(); 87 for (;;) { 88 wait(waitTime); 89 if (count_ <= 0) 90 return true; 91 else { 92 waitTime = msecs - (System.currentTimeMillis() - start); 93 if (waitTime <= 0) 94 return false; 95 } 96 } 97 } 98 } 99 } 100 101 106 public synchronized void release() { 107 if (--count_ == 0) 108 notifyAll(); 109 } 110 111 112 public int initialCount() { return initialCount_; } 113 114 115 120 public synchronized int currentCount() { return count_; } 121 } 122 123 | Popular Tags |