ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/CountDownLatch.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/CountDownLatch.java (file contents):
Revision 1.1 by tim, Wed May 14 21:30:46 2003 UTC vs.
Revision 1.2 by dl, Tue May 27 18:14:39 2003 UTC

# Line 1 | Line 1
1 + /*
2 + * Written by Doug Lea with assistance from members of JCP JSR-166
3 + * Expert Group and released to the public domain. Use, modify, and
4 + * redistribute this code in any way without acknowledgement.
5 + */
6 +
7   package java.util.concurrent;
8  
9   /**
# Line 76 | Line 82 | package java.util.concurrent;
82   * @editor $Author$
83   */
84   public class CountDownLatch {
85 +    private final ReentrantLock lock = new ReentrantLock();
86 +    private final Condition zero = lock.newCondition();
87 +    private int count;
88  
89      /**
90       * Constructs a <tt>CountDownLatch</tt> initialized with the given
# Line 84 | Line 93 | public class CountDownLatch {
93       * @param count the number of times {@link #countDown} must be invoked
94       * before threads can pass through {@link #await}.
95       *
96 <     * @throws IllegalArgumentException if <tt>count</tt> is less than one.
96 >     * @throws IllegalArgumentException if <tt>count</tt> is less than zero.
97       */
98 <    public CountDownLatch(int count) {}
98 >    public CountDownLatch(int count) {
99 >        if (count < 0) throw new IllegalArgumentException("count < 0");
100 >        this.count = count;
101 >    }
102  
103      /**
104       * Causes the current thread to wait until the latch has counted down to
# Line 114 | Line 126 | public class CountDownLatch {
126       * @throws InterruptedException if the current thread is interrupted
127       * while waiting.
128       */
129 <    public void await() throws InterruptedException {}
129 >    public void await() throws InterruptedException {
130 >        lock.lock();
131 >        try {
132 >            while (count != 0)
133 >                zero.await();
134 >        }
135 >        finally {
136 >            lock.unlock();
137 >        }
138 >    }
139 >
140  
141      /**
142       * Causes the current thread to wait until the latch has counted down to
# Line 150 | Line 172 | public class CountDownLatch {
172       * less than or equal to zero, the method will not wait at all.
173       *
174       * @param timeout the maximum time to wait
175 <     * @param granularity the time unit of the <tt>timeout</tt> argument.
175 >     * @param unit the time unit of the <tt>timeout</tt> argument.
176       * @return <tt>true</tt> if the count reached zero  and <tt>false</tt>
177       * if the waiting time elapsed before the count reached zero.
178       *
179       * @throws InterruptedException if the current thread is interrupted
180       * while waiting.
181       */
182 <    public boolean await(long timeout, TimeUnit granularity)
182 >    public boolean await(long timeout, TimeUnit unit)
183          throws InterruptedException {
184 <        return false;
184 >        long nanos = unit.toNanos(timeout);
185 >        lock.lock();
186 >        try {
187 >            for (;;) {
188 >                if (count == 0)
189 >                    return true;
190 >                nanos = zero.awaitNanos(nanos);
191 >                if (nanos <= 0)
192 >                    return false;
193 >            }
194 >        }
195 >        finally {
196 >            lock.unlock();
197 >        }
198      }
199  
200  
201 +
202      /**
203       * Decrements the count of the latch, releasing all waiting threads if
204       * the count reaches zero.
# Line 172 | Line 208 | public class CountDownLatch {
208       * <p>If the current {@link #getCount count} equals zero then nothing
209       * happens.
210       */
211 <    public void countDown() {}
211 >    public void countDown() {
212 >        lock.lock();
213 >        try {
214 >            if (count > 0 && --count == 0)
215 >                zero.signalAll();
216 >        }
217 >        finally {
218 >            lock.unlock();
219 >        }
220 >    }
221  
222      /**
223       * Returns the current count.
# Line 180 | Line 225 | public class CountDownLatch {
225       * @return the current count.
226       */
227      public long getCount() {
228 <        return 0;
228 >        lock.lock();
229 >        try {
230 >            return count;
231 >        }
232 >        finally {
233 >            lock.unlock();
234 >        }
235      }
236   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines