< prev index next >

src/java.base/share/classes/java/util/concurrent/CountDownLatch.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


 101  *       startSignal.await();
 102  *       doWork();
 103  *       doneSignal.countDown();
 104  *     } catch (InterruptedException ex) {} // return;
 105  *   }
 106  *
 107  *   void doWork() { ... }
 108  * }}</pre>
 109  *
 110  * <p>Another typical usage would be to divide a problem into N parts,
 111  * describe each part with a Runnable that executes that portion and
 112  * counts down on the latch, and queue all the Runnables to an
 113  * Executor.  When all sub-parts are complete, the coordinating thread
 114  * will be able to pass through await. (When threads must repeatedly
 115  * count down in this way, instead use a {@link CyclicBarrier}.)
 116  *
 117  * <pre> {@code
 118  * class Driver2 { // ...
 119  *   void main() throws InterruptedException {
 120  *     CountDownLatch doneSignal = new CountDownLatch(N);
 121  *     Executor e = ...
 122  *
 123  *     for (int i = 0; i < N; ++i) // create and start threads
 124  *       e.execute(new WorkerRunnable(doneSignal, i));
 125  *
 126  *     doneSignal.await();           // wait for all to finish
 127  *   }
 128  * }
 129  *
 130  * class WorkerRunnable implements Runnable {
 131  *   private final CountDownLatch doneSignal;
 132  *   private final int i;
 133  *   WorkerRunnable(CountDownLatch doneSignal, int i) {
 134  *     this.doneSignal = doneSignal;
 135  *     this.i = i;
 136  *   }
 137  *   public void run() {
 138  *     try {
 139  *       doWork(i);
 140  *       doneSignal.countDown();
 141  *     } catch (InterruptedException ex) {} // return;
 142  *   }
 143  *
 144  *   void doWork() { ... }
 145  * }}</pre>
 146  *
 147  * <p>Memory consistency effects: Until the count reaches
 148  * zero, actions in a thread prior to calling
 149  * {@code countDown()}
 150  * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
 151  * actions following a successful return from a corresponding
 152  * {@code await()} in another thread.
 153  *
 154  * @since 1.5
 155  * @author Doug Lea
 156  */
 157 public class CountDownLatch {
 158     /**
 159      * Synchronization control For CountDownLatch.
 160      * Uses AQS state to represent count.
 161      */




 101  *       startSignal.await();
 102  *       doWork();
 103  *       doneSignal.countDown();
 104  *     } catch (InterruptedException ex) {} // return;
 105  *   }
 106  *
 107  *   void doWork() { ... }
 108  * }}</pre>
 109  *
 110  * <p>Another typical usage would be to divide a problem into N parts,
 111  * describe each part with a Runnable that executes that portion and
 112  * counts down on the latch, and queue all the Runnables to an
 113  * Executor.  When all sub-parts are complete, the coordinating thread
 114  * will be able to pass through await. (When threads must repeatedly
 115  * count down in this way, instead use a {@link CyclicBarrier}.)
 116  *
 117  * <pre> {@code
 118  * class Driver2 { // ...
 119  *   void main() throws InterruptedException {
 120  *     CountDownLatch doneSignal = new CountDownLatch(N);
 121  *     Executor e = ...;
 122  *
 123  *     for (int i = 0; i < N; ++i) // create and start threads
 124  *       e.execute(new WorkerRunnable(doneSignal, i));
 125  *
 126  *     doneSignal.await();           // wait for all to finish
 127  *   }
 128  * }
 129  *
 130  * class WorkerRunnable implements Runnable {
 131  *   private final CountDownLatch doneSignal;
 132  *   private final int i;
 133  *   WorkerRunnable(CountDownLatch doneSignal, int i) {
 134  *     this.doneSignal = doneSignal;
 135  *     this.i = i;
 136  *   }
 137  *   public void run() {
 138  *     doWork();

 139  *     doneSignal.countDown();

 140  *   }
 141  *
 142  *   void doWork() { ... }
 143  * }}</pre>
 144  *
 145  * <p>Memory consistency effects: Until the count reaches
 146  * zero, actions in a thread prior to calling
 147  * {@code countDown()}
 148  * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
 149  * actions following a successful return from a corresponding
 150  * {@code await()} in another thread.
 151  *
 152  * @since 1.5
 153  * @author Doug Lea
 154  */
 155 public class CountDownLatch {
 156     /**
 157      * Synchronization control For CountDownLatch.
 158      * Uses AQS state to represent count.
 159      */


< prev index next >