jdk/src/share/classes/java/util/concurrent/CyclicBarrier.java
author smarks
Tue, 06 Dec 2011 10:14:02 -0800
changeset 11139 db0c2ff5e1ea
parent 9242 ef138d47df58
child 14325 622c473a21aa
permissions -rw-r--r--
7116997: fix warnings in java.util.PropertyPermission Reviewed-by: smarks Contributed-by: Brandon Passanisi <brandon.passanisi@oracle.com>
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: 2
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: 2
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: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 5506
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A synchronization aid that allows a set of threads to all wait for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * each other to reach a common barrier point.  CyclicBarriers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * useful in programs involving a fixed sized party of threads that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * must occasionally wait for each other. The barrier is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <em>cyclic</em> because it can be re-used after the waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * are released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>A <tt>CyclicBarrier</tt> supports an optional {@link Runnable} command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * that is run once per barrier point, after the last thread in the party
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * arrives, but before any threads are released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This <em>barrier action</em> is useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * for updating shared-state before any of the parties continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p><b>Sample usage:</b> Here is an example of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  using a barrier in a parallel decomposition design:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * class Solver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   final int N;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   final float[][] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   final CyclicBarrier barrier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   class Worker implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     int myRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     Worker(int row) { myRow = row; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *       while (!done()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         processRow(myRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *           barrier.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         } catch (BrokenBarrierException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   public Solver(float[][] matrix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     data = matrix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     N = matrix.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     barrier = new CyclicBarrier(N,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *                                 new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *                                   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *                                     mergeRows(...);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *                                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *                                 });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     for (int i = 0; i < N; ++i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *       new Thread(new Worker(i)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *     waitUntilDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Here, each worker thread processes a row of the matrix then waits at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * barrier until all rows have been processed. When all rows are processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * the supplied {@link Runnable} barrier action is executed and merges the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * rows. If the merger
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * determines that a solution has been found then <tt>done()</tt> will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <tt>true</tt> and each worker will terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>If the barrier action does not rely on the parties being suspended when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * it is executed, then any of the threads in the party could execute that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * action when it is released. To facilitate this, each invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * {@link #await} returns the arrival index of that thread at the barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * You can then choose which thread should execute the barrier action, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * <pre>  if (barrier.await() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *     // log the completion of this iteration
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>The <tt>CyclicBarrier</tt> uses an all-or-none breakage model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * for failed synchronization attempts: If a thread leaves a barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * point prematurely because of interruption, failure, or timeout, all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * other threads waiting at that barrier point will also leave
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * abnormally via {@link BrokenBarrierException} (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * {@link InterruptedException} if they too were interrupted at about
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * the same time).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>Memory consistency effects: Actions in a thread prior to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * {@code await()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * actions that are part of the barrier action, which in turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <i>happen-before</i> actions following a successful return from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * corresponding {@code await()} in other threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see CountDownLatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
public class CyclicBarrier {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Each use of the barrier is represented as a generation instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * The generation changes whenever the barrier is tripped, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * is reset. There can be many generations associated with threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * using the barrier - due to the non-deterministic way the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * may be allocated to waiting threads - but only one of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * can be active at a time (the one to which <tt>count</tt> applies)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * and all the rest are either broken or tripped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * There need not be an active generation if there has been a break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * but no subsequent reset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private static class Generation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        boolean broken = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /** The lock for guarding barrier entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /** Condition to wait on until tripped */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private final Condition trip = lock.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /** The number of parties */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private final int parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /* The command to run when tripped */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private final Runnable barrierCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /** The current generation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private Generation generation = new Generation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Number of parties still waiting. Counts down from parties to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * on each generation.  It is reset to parties on each new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * generation or when broken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Updates state on barrier trip and wakes up everyone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Called only while holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private void nextGeneration() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // signal completion of last generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        trip.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // set up next generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        count = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        generation = new Generation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Sets current barrier generation as broken and wakes up everyone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Called only while holding lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private void breakBarrier() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        generation.broken = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        count = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        trip.signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Main barrier code, covering the various policies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private int dowait(boolean timed, long nanos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        throws InterruptedException, BrokenBarrierException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
               TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            final Generation g = generation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (g.broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw new BrokenBarrierException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (Thread.interrupted()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                throw new InterruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
           int index = --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
           if (index == 0) {  // tripped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
               boolean ranAction = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
               try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                   final Runnable command = barrierCommand;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                   if (command != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                       command.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                   ranAction = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                   nextGeneration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                   return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
               } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                   if (!ranAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                       breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // loop until tripped, broken, interrupted, or timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    if (!timed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        trip.await();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    else if (nanos > 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        nanos = trip.awaitNanos(nanos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    if (g == generation && ! g.broken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                        throw ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        // We're about to finish waiting even if we had not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                        // been interrupted, so this interrupt is deemed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        // "belong" to subsequent execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                if (g.broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    throw new BrokenBarrierException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (g != generation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (timed && nanos <= 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    breakBarrier();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    throw new TimeoutException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Creates a new <tt>CyclicBarrier</tt> that will trip when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * given number of parties (threads) are waiting upon it, and which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * will execute the given barrier action when the barrier is tripped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * performed by the last thread entering the barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param parties the number of threads that must invoke {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *        before the barrier is tripped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param barrierAction the command to execute when the barrier is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *        tripped, or {@code null} if there is no action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @throws IllegalArgumentException if {@code parties} is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public CyclicBarrier(int parties, Runnable barrierAction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (parties <= 0) throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        this.parties = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.count = parties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        this.barrierCommand = barrierAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Creates a new <tt>CyclicBarrier</tt> that will trip when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * given number of parties (threads) are waiting upon it, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * does not perform a predefined action when the barrier is tripped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param parties the number of threads that must invoke {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *        before the barrier is tripped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @throws IllegalArgumentException if {@code parties} is less than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public CyclicBarrier(int parties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        this(parties, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the number of parties required to trip this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @return the number of parties required to trip this barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public int getParties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return parties;
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
     * Waits until all {@linkplain #getParties parties} have invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <tt>await</tt> on this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p>If the current thread is not the last to arrive then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * one of the following things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <li>The last thread arrives; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * one of the other waiting threads; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * <li>Some other thread times out while waiting for barrier; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <li>Some other thread invokes {@link #reset} on this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * <p>If the barrier is {@link #reset} while any thread is waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * or if the barrier {@linkplain #isBroken is broken} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <tt>await</tt> is invoked, or while any thread is waiting, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * {@link BrokenBarrierException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p>If any thread is {@linkplain Thread#interrupt interrupted} while waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * then all other waiting threads will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * {@link BrokenBarrierException} and the barrier is placed in the broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>If the current thread is the last thread to arrive, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * non-null barrier action was supplied in the constructor, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * current thread runs the action before allowing the other threads to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * If an exception occurs during the barrier action then that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * will be propagated in the current thread and the barrier is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * the broken state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @return the arrival index of the current thread, where index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *         <tt>{@link #getParties()} - 1</tt> indicates the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *         to arrive and zero indicates the last to arrive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @throws BrokenBarrierException if <em>another</em> thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *         interrupted or timed out while the current thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *         waiting, or the barrier was reset, or the barrier was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *         broken when {@code await} was called, or the barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *         action (if present) failed due an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public int await() throws InterruptedException, BrokenBarrierException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return dowait(false, 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } catch (TimeoutException toe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            throw new Error(toe); // cannot happen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Waits until all {@linkplain #getParties parties} have invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <tt>await</tt> on this barrier, or the specified waiting time elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p>If the current thread is not the last to arrive then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * one of the following things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <li>The last thread arrives; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <li>The specified timeout elapses; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * one of the other waiting threads; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <li>Some other thread times out while waiting for barrier; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <li>Some other thread invokes {@link #reset} on this barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>If the specified waiting time elapses then {@link TimeoutException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * is thrown. If the time is less than or equal to zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * method will not wait at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <p>If the barrier is {@link #reset} while any thread is waiting,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * or if the barrier {@linkplain #isBroken is broken} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <tt>await</tt> is invoked, or while any thread is waiting, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * {@link BrokenBarrierException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * <p>If any thread is {@linkplain Thread#interrupt interrupted} while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * waiting, then all other waiting threads will throw {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * BrokenBarrierException} and the barrier is placed in the broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <p>If the current thread is the last thread to arrive, and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * non-null barrier action was supplied in the constructor, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * current thread runs the action before allowing the other threads to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * continue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * If an exception occurs during the barrier action then that exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * will be propagated in the current thread and the barrier is placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * the broken state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param timeout the time to wait for the barrier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param unit the time unit of the timeout parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @return the arrival index of the current thread, where index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *         <tt>{@link #getParties()} - 1</tt> indicates the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *         to arrive and zero indicates the last to arrive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *         while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @throws TimeoutException if the specified timeout elapses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @throws BrokenBarrierException if <em>another</em> thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         interrupted or timed out while the current thread was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *         waiting, or the barrier was reset, or the barrier was broken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *         when {@code await} was called, or the barrier action (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *         present) failed due an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    public int await(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        throws InterruptedException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
               BrokenBarrierException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
               TimeoutException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        return dowait(true, unit.toNanos(timeout));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Queries if this barrier is in a broken state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @return {@code true} if one or more parties broke out of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *         barrier due to interruption or timeout since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *         construction or the last reset, or a barrier action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         failed due to an exception; {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public boolean isBroken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return generation.broken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Resets the barrier to its initial state.  If any parties are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * currently waiting at the barrier, they will return with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * {@link BrokenBarrierException}. Note that resets <em>after</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * a breakage has occurred for other reasons can be complicated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * carry out; threads need to re-synchronize in some other way,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * and choose one to perform the reset.  It may be preferable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * instead create a new barrier for subsequent use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            breakBarrier();   // break the current generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            nextGeneration(); // start a new generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Returns the number of parties currently waiting at the barrier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * This method is primarily useful for debugging and assertions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @return the number of parties currently blocked in {@link #await}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public int getNumberWaiting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        final ReentrantLock lock = this.lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            return parties - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
}