jdk/src/share/classes/java/util/concurrent/CountDownLatch.java
author dl
Wed, 01 Dec 2010 21:46:52 +0000
changeset 7518 0282db800fe1
parent 5506 202f599c92aa
child 9242 ef138d47df58
permissions -rw-r--r--
7003745: Code style cleanups (sync from Dougs CVS) Reviewed-by: chegar, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * http://creativecommons.org/licenses/publicdomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.concurrent.locks.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A synchronization aid that allows one or more threads to wait until
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * a set of operations being performed in other threads completes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>A {@code CountDownLatch} is initialized with a given <em>count</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The {@link #await await} methods block until the current count reaches
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * zero due to invocations of the {@link #countDown} method, after which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * all waiting threads are released and any subsequent invocations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@link #await await} return immediately.  This is a one-shot phenomenon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * -- the count cannot be reset.  If you need a version that resets the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * count, consider using a {@link CyclicBarrier}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>A {@code CountDownLatch} is a versatile synchronization tool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * and can be used for a number of purposes.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@code CountDownLatch} initialized with a count of one serves as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * simple on/off latch, or gate: all threads invoking {@link #await await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * wait at the gate until it is opened by a thread invoking {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * #countDown}.  A {@code CountDownLatch} initialized to <em>N</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * can be used to make one thread wait until <em>N</em> threads have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * completed some action, or some action has been completed N times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>A useful property of a {@code CountDownLatch} is that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * doesn't require that threads calling {@code countDown} wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * the count to reach zero before proceeding, it simply prevents any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * thread from proceeding past an {@link #await await} until all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * threads could pass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p><b>Sample usage:</b> Here is a pair of classes in which a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * of worker threads use two countdown latches:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <li>The first is a start signal that prevents any worker from proceeding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * until the driver is ready for them to proceed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <li>The second is a completion signal that allows the driver to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * until all workers have completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * class Driver { // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   void main() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     CountDownLatch startSignal = new CountDownLatch(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     CountDownLatch doneSignal = new CountDownLatch(N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     for (int i = 0; i < N; ++i) // create and start threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *       new Thread(new Worker(startSignal, doneSignal)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     doSomethingElse();            // don't let run yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     startSignal.countDown();      // let all threads proceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     doSomethingElse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     doneSignal.await();           // wait for all to finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * class Worker implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   private final CountDownLatch startSignal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   private final CountDownLatch doneSignal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   Worker(CountDownLatch startSignal, CountDownLatch doneSignal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      this.startSignal = startSignal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *      this.doneSignal = doneSignal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *        startSignal.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *        doWork();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *        doneSignal.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *      } catch (InterruptedException ex) {} // return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   void doWork() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>Another typical usage would be to divide a problem into N parts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * describe each part with a Runnable that executes that portion and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * counts down on the latch, and queue all the Runnables to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Executor.  When all sub-parts are complete, the coordinating thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * will be able to pass through await. (When threads must repeatedly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * count down in this way, instead use a {@link CyclicBarrier}.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * class Driver2 { // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *   void main() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     CountDownLatch doneSignal = new CountDownLatch(N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *     Executor e = ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *     for (int i = 0; i < N; ++i) // create and start threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *       e.execute(new WorkerRunnable(doneSignal, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *     doneSignal.await();           // wait for all to finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * class WorkerRunnable implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *   private final CountDownLatch doneSignal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *   private final int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *   WorkerRunnable(CountDownLatch doneSignal, int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *      this.doneSignal = doneSignal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *      this.i = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *        doWork(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *        doneSignal.countDown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *      } catch (InterruptedException ex) {} // return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *   void doWork() { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   151
 * <p>Memory consistency effects: Until the count reaches
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   152
 * zero, actions in a thread prior to calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * {@code countDown()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * actions following a successful return from a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * {@code await()} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
public class CountDownLatch {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Synchronization control For CountDownLatch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Uses AQS state to represent count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static final class Sync extends AbstractQueuedSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        private static final long serialVersionUID = 4982264981922014374L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Sync(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            setState(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        protected int tryAcquireShared(int acquires) {
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   178
            return (getState() == 0) ? 1 : -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        protected boolean tryReleaseShared(int releases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // Decrement count; signal when transition to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                int c = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                if (c == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                int nextc = c-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                if (compareAndSetState(c, nextc))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    return nextc == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private final Sync sync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Constructs a {@code CountDownLatch} initialized with the given count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param count the number of times {@link #countDown} must be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *        before threads can pass through {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @throws IllegalArgumentException if {@code count} is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public CountDownLatch(int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (count < 0) throw new IllegalArgumentException("count < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.sync = new Sync(count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Causes the current thread to wait until the latch has counted down to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * zero, unless the thread is {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>If the current count is zero then this method returns immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p>If the current count is greater than zero then the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * thread becomes disabled for thread scheduling purposes and lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * dormant until one of two things happen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <li>The count reaches zero due to invocations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * {@link #countDown} method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *         while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void await() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        sync.acquireSharedInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Causes the current thread to wait until the latch has counted down to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * zero, unless the thread is {@linkplain Thread#interrupt interrupted},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * or the specified waiting time elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p>If the current count is zero then this method returns immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * with the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <p>If the current count is greater than zero then the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * thread becomes disabled for thread scheduling purposes and lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * dormant until one of three things happen:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <li>The count reaches zero due to invocations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * {@link #countDown} method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <li>The specified waiting time elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p>If the count reaches zero then the method returns with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <p>If the specified waiting time elapses then the value {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * is returned.  If the time is less than or equal to zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * will not wait at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param timeout the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param unit the time unit of the {@code timeout} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return {@code true} if the count reached zero and {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *         if the waiting time elapsed before the count reached zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *         while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public boolean await(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Decrements the count of the latch, releasing all waiting threads if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * the count reaches zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>If the current count is greater than zero then it is decremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * If the new count is zero then all waiting threads are re-enabled for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * thread scheduling purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>If the current count equals zero then nothing happens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public void countDown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        sync.releaseShared(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Returns the current count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>This method is typically used for debugging and testing purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return the current count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public long getCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return sync.getCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Returns a string identifying this latch, as well as its state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * The state, in brackets, includes the String {@code "Count ="}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * followed by the current count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @return a string identifying this latch, as well as its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return super.toString() + "[Count = " + sync.getCount() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
}