1 *java.util.concurrent.CountDownLatch* *CountDownLatch* A synchronization aid tha
3 public class CountDownLatch
4 extends |java.lang.Object|
6 |java.util.concurrent.CountDownLatch_Description|
7 |java.util.concurrent.CountDownLatch_Fields|
8 |java.util.concurrent.CountDownLatch_Constructors|
9 |java.util.concurrent.CountDownLatch_Methods|
11 ================================================================================
13 *java.util.concurrent.CountDownLatch_Constructors*
14 |java.util.concurrent.CountDownLatch(int)|Constructs a CountDownLatch initializ
16 *java.util.concurrent.CountDownLatch_Methods*
17 |java.util.concurrent.CountDownLatch.await()|Causes the current thread to wait
18 |java.util.concurrent.CountDownLatch.await(long,TimeUnit)|Causes the current th
19 |java.util.concurrent.CountDownLatch.countDown()|Decrements the count of the la
20 |java.util.concurrent.CountDownLatch.getCount()|Returns the current count.
21 |java.util.concurrent.CountDownLatch.toString()|Returns a string identifying th
23 *java.util.concurrent.CountDownLatch_Description*
25 A synchronization aid that allows one or more threads to wait until a set of
26 operations being performed in other threads completes.
28 A CountDownLatch is initialized with a given count. The
29 await(|java.util.concurrent.CountDownLatch|) methods block until the current
30 count(|java.util.concurrent.CountDownLatch|) reaches zero due to invocations of
31 the (|java.util.concurrent.CountDownLatch|) method, after which all waiting
32 threads are released and any subsequent invocations of
33 await(|java.util.concurrent.CountDownLatch|) return immediately. This is a
34 one-shot phenomenon -- the count cannot be reset. If you need a version that
35 resets the count, consider using a (|java.util.concurrent.CyclicBarrier|) .
37 A CountDownLatch is a versatile synchronization tool and can be used for a
38 number of purposes. A CountDownLatch initialized with a count of one serves as
39 a simple on/off latch, or gate: all threads invoking
40 await(|java.util.concurrent.CountDownLatch|) wait at the gate until it is
41 opened by a thread invoking (|java.util.concurrent.CountDownLatch|) . A
42 CountDownLatch initialized to N can be used to make one thread wait until N
43 threads have completed some action, or some action has been completed N times.
44 A useful property of a CountDownLatch is that it doesn't require that threads
45 calling countDown wait for the count to reach zero before proceeding, it simply
46 prevents any thread from proceeding past an
47 await(|java.util.concurrent.CountDownLatch|) until all threads could pass.
49 Sample usage: Here is a pair of classes in which a group of worker threads use
50 two countdown latches:
52 The first is a start signal that prevents any worker from proceeding until the
53 driver is ready for them to proceed; The second is a completion signal that
54 allows the driver to wait until all workers have completed.
58 class Driver { // ... void main() throws InterruptedException { CountDownLatch
59 startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new
62 for (int i = 0; i Another typical usage would be to divide a problem into N
63 parts, describe each part with a Runnable that executes that portion and counts
64 down on the latch, and queue all the Runnables to an Executor. When all
65 sub-parts are complete, the coordinating thread will be able to pass through
66 await. (When threads must repeatedly count down in this way, instead use a
67 (|java.util.concurrent.CyclicBarrier|) .)
71 class Driver2 { // ... void main() throws InterruptedException { CountDownLatch
72 doneSignal = new CountDownLatch(N); Executor e = ...
74 for (int i = 0; i < N; ++i) // create and start threads e.execute(new
75 WorkerRunnable(doneSignal, i));
77 doneSignal.await(); // wait for all to finish } }
79 class WorkerRunnable implements Runnable { private final CountDownLatch
80 doneSignal; private final int i; WorkerRunnable(CountDownLatch doneSignal, int
81 i) { this.doneSignal = doneSignal; this.i = i; } public void run() { try {
82 doWork(i); doneSignal.countDown(); } catch (InterruptedException ex) {} //
85 void doWork() { ... } }
90 *java.util.concurrent.CountDownLatch(int)*
92 public CountDownLatch(int count)
94 Constructs a CountDownLatch initialized with the given count.
96 count - the number of times {@link #countDown} must be invoked before threads can pass
97 through {@link #await}.
99 *java.util.concurrent.CountDownLatch.await()*
102 throws |java.lang.InterruptedException|
104 Causes the current thread to wait until the latch has counted down to zero,
105 unless the thread is interrupted(|java.lang.Thread|) .
107 If the current count(|java.util.concurrent.CountDownLatch|) is zero then this
108 method returns immediately. If the current
109 count(|java.util.concurrent.CountDownLatch|) is greater than zero then the
110 current thread becomes disabled for thread scheduling purposes and lies dormant
111 until one of two things happen:
113 The count reaches zero due to invocations of the
114 (|java.util.concurrent.CountDownLatch|) method; or Some other thread
115 interrupts(|java.lang.Thread|) the current thread.
117 If the current thread:
119 has its interrupted status set on entry to this method; or is
120 interrupted(|java.lang.Thread|) while waiting,
122 then (|java.lang.InterruptedException|) is thrown and the current thread's
123 interrupted status is cleared.
126 *java.util.concurrent.CountDownLatch.await(long,TimeUnit)*
128 public boolean await(
130 java.util.concurrent.TimeUnit unit)
131 throws |java.lang.InterruptedException|
133 Causes the current thread to wait until the latch has counted down to zero,
134 unless the thread is interrupted(|java.lang.Thread|) , or the specified waiting
137 If the current count(|java.util.concurrent.CountDownLatch|) is zero then this
138 method returns immediately with the value true.
140 If the current count(|java.util.concurrent.CountDownLatch|) is greater than
141 zero then the current thread becomes disabled for thread scheduling purposes
142 and lies dormant until one of three things happen:
144 The count reaches zero due to invocations of the
145 (|java.util.concurrent.CountDownLatch|) method; or Some other thread
146 interrupts(|java.lang.Thread|) the current thread; or The specified waiting
149 If the count reaches zero then the method returns with the value true. If the
152 has its interrupted status set on entry to this method; or is
153 interrupted(|java.lang.Thread|) while waiting,
155 then (|java.lang.InterruptedException|) is thrown and the current thread's
156 interrupted status is cleared.
158 If the specified waiting time elapses then the value false is returned. If the
159 time is less than or equal to zero, the method will not wait at all.
161 timeout - the maximum time to wait
162 unit - the time unit of the timeout argument.
164 Returns: true if the count reached zero and false if the waiting time elapsed before the
166 *java.util.concurrent.CountDownLatch.countDown()*
168 public void countDown()
170 Decrements the count of the latch, releasing all waiting threads if the count
171 reaches zero. If the current count(|java.util.concurrent.CountDownLatch|) is
172 greater than zero then it is decremented. If the new count is zero then all
173 waiting threads are re-enabled for thread scheduling purposes. If the current
174 count(|java.util.concurrent.CountDownLatch|) equals zero then nothing happens.
177 *java.util.concurrent.CountDownLatch.getCount()*
179 public long getCount()
181 Returns the current count. This method is typically used for debugging and
185 Returns: the current count.
186 *java.util.concurrent.CountDownLatch.toString()*
188 public |java.lang.String| toString()
190 Returns a string identifying this latch, as well as its state. The state, in
191 brackets, includes the String Count = followed by the current count.
194 Returns: a string identifying this latch, as well as its state