32 * Expert Group and released to the public domain, as explained at |
32 * Expert Group and released to the public domain, as explained at |
33 * http://creativecommons.org/publicdomain/zero/1.0/ |
33 * http://creativecommons.org/publicdomain/zero/1.0/ |
34 */ |
34 */ |
35 |
35 |
36 package java.util.concurrent; |
36 package java.util.concurrent; |
37 import java.util.concurrent.locks.*; |
37 import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
38 import java.util.concurrent.atomic.*; |
|
39 |
38 |
40 /** |
39 /** |
41 * A synchronization aid that allows one or more threads to wait until |
40 * A synchronization aid that allows one or more threads to wait until |
42 * a set of operations being performed in other threads completes. |
41 * a set of operations being performed in other threads completes. |
43 * |
42 * |
71 * until the driver is ready for them to proceed; |
70 * until the driver is ready for them to proceed; |
72 * <li>The second is a completion signal that allows the driver to wait |
71 * <li>The second is a completion signal that allows the driver to wait |
73 * until all workers have completed. |
72 * until all workers have completed. |
74 * </ul> |
73 * </ul> |
75 * |
74 * |
76 * <pre> |
75 * <pre> {@code |
77 * class Driver { // ... |
76 * class Driver { // ... |
78 * void main() throws InterruptedException { |
77 * void main() throws InterruptedException { |
79 * CountDownLatch startSignal = new CountDownLatch(1); |
78 * CountDownLatch startSignal = new CountDownLatch(1); |
80 * CountDownLatch doneSignal = new CountDownLatch(N); |
79 * CountDownLatch doneSignal = new CountDownLatch(N); |
81 * |
80 * |
103 * doneSignal.countDown(); |
102 * doneSignal.countDown(); |
104 * } catch (InterruptedException ex) {} // return; |
103 * } catch (InterruptedException ex) {} // return; |
105 * } |
104 * } |
106 * |
105 * |
107 * void doWork() { ... } |
106 * void doWork() { ... } |
108 * } |
107 * }}</pre> |
109 * |
|
110 * </pre> |
|
111 * |
108 * |
112 * <p>Another typical usage would be to divide a problem into N parts, |
109 * <p>Another typical usage would be to divide a problem into N parts, |
113 * describe each part with a Runnable that executes that portion and |
110 * describe each part with a Runnable that executes that portion and |
114 * counts down on the latch, and queue all the Runnables to an |
111 * counts down on the latch, and queue all the Runnables to an |
115 * Executor. When all sub-parts are complete, the coordinating thread |
112 * Executor. When all sub-parts are complete, the coordinating thread |
116 * will be able to pass through await. (When threads must repeatedly |
113 * will be able to pass through await. (When threads must repeatedly |
117 * count down in this way, instead use a {@link CyclicBarrier}.) |
114 * count down in this way, instead use a {@link CyclicBarrier}.) |
118 * |
115 * |
119 * <pre> |
116 * <pre> {@code |
120 * class Driver2 { // ... |
117 * class Driver2 { // ... |
121 * void main() throws InterruptedException { |
118 * void main() throws InterruptedException { |
122 * CountDownLatch doneSignal = new CountDownLatch(N); |
119 * CountDownLatch doneSignal = new CountDownLatch(N); |
123 * Executor e = ... |
120 * Executor e = ... |
124 * |
121 * |
142 * doneSignal.countDown(); |
139 * doneSignal.countDown(); |
143 * } catch (InterruptedException ex) {} // return; |
140 * } catch (InterruptedException ex) {} // return; |
144 * } |
141 * } |
145 * |
142 * |
146 * void doWork() { ... } |
143 * void doWork() { ... } |
147 * } |
144 * }}</pre> |
148 * |
|
149 * </pre> |
|
150 * |
145 * |
151 * <p>Memory consistency effects: Until the count reaches |
146 * <p>Memory consistency effects: Until the count reaches |
152 * zero, actions in a thread prior to calling |
147 * zero, actions in a thread prior to calling |
153 * {@code countDown()} |
148 * {@code countDown()} |
154 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
149 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |