jdk/src/share/classes/java/util/concurrent/locks/ReentrantLock.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 7518 0282db800fe1
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
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
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.locks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.concurrent.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.concurrent.atomic.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A reentrant mutual exclusion {@link Lock} with the same basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * behavior and semantics as the implicit monitor lock accessed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@code synchronized} methods and statements, but with extended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * capabilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>A {@code ReentrantLock} is <em>owned</em> by the thread last
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * successfully locking, but not yet unlocking it. A thread invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@code lock} will return, successfully acquiring the lock, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * the lock is not owned by another thread. The method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * immediately if the current thread already owns the lock. This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * be checked using methods {@link #isHeldByCurrentThread}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * #getHoldCount}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>The constructor for this class accepts an optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <em>fairness</em> parameter.  When set {@code true}, under
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * contention, locks favor granting access to the longest-waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * thread.  Otherwise this lock does not guarantee any particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * access order.  Programs using fair locks accessed by many threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * may display lower overall throughput (i.e., are slower; often much
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * slower) than those using the default setting, but have smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * variances in times to obtain locks and guarantee lack of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * starvation. Note however, that fairness of locks does not guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * fairness of thread scheduling. Thus, one of many threads using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * fair lock may obtain it multiple times in succession while other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * active threads are not progressing and not currently holding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Also note that the untimed {@link #tryLock() tryLock} method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * honor the fairness setting. It will succeed if the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * is available even if other threads are waiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>It is recommended practice to <em>always</em> immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * follow a call to {@code lock} with a {@code try} block, most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * typically in a before/after construction such as:
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 X {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   private final ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   public void m() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     lock.lock();  // block until condition holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *       // ... method body
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *       lock.unlock()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>In addition to implementing the {@link Lock} interface, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * class defines methods {@code isLocked} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * {@code getLockQueueLength}, as well as some associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * {@code protected} access methods that may be useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * instrumentation and monitoring.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p>Serialization of this class behaves in the same way as built-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * locks: a deserialized lock is in the unlocked state, regardless of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * its state when serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>This lock supports a maximum of 2147483647 recursive locks by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * the same thread. Attempts to exceed this limit result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * {@link Error} throws from locking methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
public class ReentrantLock implements Lock, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private static final long serialVersionUID = 7373984872572414699L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /** Synchronizer providing all implementation mechanics */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private final Sync sync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Base of synchronization control for this lock. Subclassed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * into fair and nonfair versions below. Uses AQS state to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * represent the number of holds on the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static abstract class Sync extends AbstractQueuedSynchronizer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        private static final long serialVersionUID = -5179523762034025860L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         * Performs {@link Lock#lock}. The main reason for subclassing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * is to allow fast path for nonfair version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        abstract void lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * Performs non-fair tryLock.  tryAcquire is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * implemented in subclasses, but both need nonfair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         * try for trylock method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        final boolean nonfairTryAcquire(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            final Thread current = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int c = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (c == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (compareAndSetState(0, acquires)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    setExclusiveOwnerThread(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            else if (current == getExclusiveOwnerThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                int nextc = c + acquires;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (nextc < 0) // overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    throw new Error("Maximum lock count exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                setState(nextc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        protected final boolean tryRelease(int releases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            int c = getState() - releases;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (Thread.currentThread() != getExclusiveOwnerThread())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new IllegalMonitorStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            boolean free = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (c == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                free = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                setExclusiveOwnerThread(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            setState(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        protected final boolean isHeldExclusively() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // While we must in general read state before owner,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            // we don't need to do so to check if current thread is owner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return getExclusiveOwnerThread() == Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        final ConditionObject newCondition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return new ConditionObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // Methods relayed from outer class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        final Thread getOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return getState() == 0 ? null : getExclusiveOwnerThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        final int getHoldCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            return isHeldExclusively() ? getState() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        final boolean isLocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return getState() != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         * Reconstitutes this lock instance from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
         * @param s the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            setState(0); // reset to unlocked state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Sync object for non-fair locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    final static class NonfairSync extends Sync {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        private static final long serialVersionUID = 7316153563782823691L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * Performs lock.  Try immediate barge, backing up to normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         * acquire on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        final void lock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (compareAndSetState(0, 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                setExclusiveOwnerThread(Thread.currentThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                acquire(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        protected final boolean tryAcquire(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return nonfairTryAcquire(acquires);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
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
     * Sync object for fair locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    final static class FairSync extends Sync {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private static final long serialVersionUID = -3000897897090466540L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        final void lock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            acquire(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         * Fair version of tryAcquire.  Don't grant access unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         * recursive call or no waiters or is first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        protected final boolean tryAcquire(int acquires) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            final Thread current = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int c = getState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (c == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                if (!hasQueuedPredecessors() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    compareAndSetState(0, acquires)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    setExclusiveOwnerThread(current);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            else if (current == getExclusiveOwnerThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                int nextc = c + acquires;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (nextc < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    throw new Error("Maximum lock count exceeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                setState(nextc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
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
     * Creates an instance of {@code ReentrantLock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * This is equivalent to using {@code ReentrantLock(false)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public ReentrantLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        sync = new NonfairSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Creates an instance of {@code ReentrantLock} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * given fairness policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param fair {@code true} if this lock should use a fair ordering policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public ReentrantLock(boolean fair) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        sync = (fair)? new FairSync() : new NonfairSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Acquires the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>Acquires the lock if it is not held by another thread and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * immediately, setting the lock hold count to one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>If the current thread already holds the lock then the hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * count is incremented by one and the method returns immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p>If the lock is held by another thread then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * purposes and lies dormant until the lock has been acquired,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * at which time the lock hold count is set to one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public void lock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        sync.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Acquires the lock unless the current thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * <p>Acquires the lock if it is not held by another thread and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * immediately, setting the lock hold count to one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>If the current thread already holds this lock then the hold count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * is incremented by one and the method returns immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p>If the lock is held by another thread then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * purposes and lies dormant until one of two things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <li>The lock is acquired by the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
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 lock is acquired by the current thread then the lock hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * count is set to one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * the lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>In this implementation, as this method is an explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * interruption point, preference is given to responding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * interrupt over normal or reentrant acquisition of the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    public void lockInterruptibly() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        sync.acquireInterruptibly(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Acquires the lock only if it is not held by another thread at the time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * of invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <p>Acquires the lock if it is not held by another thread and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * returns immediately with the value {@code true}, setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * lock hold count to one. Even when this lock has been set to use a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * fair ordering policy, a call to {@code tryLock()} <em>will</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * immediately acquire the lock if it is available, whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * other threads are currently waiting for the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * This &quot;barging&quot; behavior can be useful in certain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * circumstances, even though it breaks fairness. If you want to honor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * the fairness setting for this lock, then use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * {@link #tryLock(long, TimeUnit) tryLock(0, TimeUnit.SECONDS) }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * which is almost equivalent (it also detects interruption).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <p> If the current thread already holds this lock then the hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * count is incremented by one and the method returns {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>If the lock is held by another thread then this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * immediately with the value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return {@code true} if the lock was free and was acquired by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *         current thread, or the lock was already held by the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *         thread; and {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public boolean tryLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return sync.nonfairTryAcquire(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Acquires the lock if it is not held by another thread within the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * waiting time and the current thread has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <p>Acquires the lock if it is not held by another thread and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * immediately with the value {@code true}, setting the lock hold count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * to one. If this lock has been set to use a fair ordering policy then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * an available lock <em>will not</em> be acquired if any other threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * are waiting for the lock. This is in contrast to the {@link #tryLock()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * method. If you want a timed {@code tryLock} that does permit barging on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * a fair lock then combine the timed and un-timed forms together:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <p>If the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * already holds this lock then the hold count is incremented by one and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * the method returns {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p>If the lock is held by another thread then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * purposes and lies dormant until one of three things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * <li>The lock is acquired by the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <li>Some other thread {@linkplain Thread#interrupt interrupts}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <li>The specified waiting time elapses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <p>If the lock is acquired then the value {@code true} is returned and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * the lock hold count is set to one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <li>is {@linkplain Thread#interrupt interrupted} while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * acquiring the lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>If the specified waiting time elapses then the value {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * is returned.  If the time is less than or equal to zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * will not wait at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <p>In this implementation, as this method is an explicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * interruption point, preference is given to responding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * interrupt over normal or reentrant acquisition of the lock, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * over reporting the elapse of the waiting time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param timeout the time to wait for the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @param unit the time unit of the timeout argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return {@code true} if the lock was free and was acquired by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *         current thread, or the lock was already held by the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *         thread; and {@code false} if the waiting time elapsed before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *         the lock could be acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @throws NullPointerException if the time unit is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return sync.tryAcquireNanos(1, unit.toNanos(timeout));
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
     * Attempts to release this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * <p>If the current thread is the holder of this lock then the hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * count is decremented.  If the hold count is now zero then the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * is released.  If the current thread is not the holder of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * lock then {@link IllegalMonitorStateException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @throws IllegalMonitorStateException if the current thread does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *         hold this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public void unlock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        sync.release(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Returns a {@link Condition} instance for use with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * {@link Lock} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p>The returned {@link Condition} instance supports the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * usages as do the {@link Object} monitor methods ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Object#wait() wait}, {@link Object#notify notify}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Object#notifyAll notifyAll}) when used with the built-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * monitor lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <li>If this lock is not held when any of the {@link Condition}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * {@linkplain Condition#await() waiting} or {@linkplain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Condition#signal signalling} methods are called, then an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * IllegalMonitorStateException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * <li>When the condition {@linkplain Condition#await() waiting}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * methods are called the lock is released and, before they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * return, the lock is reacquired and the lock hold count restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * to what it was when the method was called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <li>If a thread is {@linkplain Thread#interrupt interrupted}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * while waiting then the wait will terminate, an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * InterruptedException} will be thrown, and the thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * interrupted status will be cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <li> Waiting threads are signalled in FIFO order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * <li>The ordering of lock reacquisition for threads returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * from waiting methods is the same as for threads initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * acquiring the lock, which is in the default case not specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * but for <em>fair</em> locks favors those threads that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * waiting the longest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @return the Condition object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public Condition newCondition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return sync.newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Queries the number of holds on this lock by the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <p>A thread has a hold on a lock for each lock action that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * matched by an unlock action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <p>The hold count information is typically only used for testing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * debugging purposes. For example, if a certain section of code should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * not be entered with the lock already held then we can assert that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * fact:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * class X {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *   ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *   // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *   public void m() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *     assert lock.getHoldCount() == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *     lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *       // ... method body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *     } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *       lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return the number of holds on this lock by the current thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *         or zero if this lock is not held by the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public int getHoldCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return sync.getHoldCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * Queries if this lock is held by the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <p>Analogous to the {@link Thread#holdsLock} method for built-in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * monitor locks, this method is typically used for debugging and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * testing. For example, a method that should only be called while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * a lock is held can assert that this is the case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * class X {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *   ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *   // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *   public void m() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *       assert lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *       // ... method body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * <p>It can also be used to ensure that a reentrant lock is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * in a non-reentrant manner, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * class X {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *   ReentrantLock lock = new ReentrantLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *   // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *   public void m() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *       assert !lock.isHeldByCurrentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *       lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *       try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *           // ... method body
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *       } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *           lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @return {@code true} if current thread holds this lock and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    public boolean isHeldByCurrentThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return sync.isHeldExclusively();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Queries if this lock is held by any thread. This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * designed for use in monitoring of the system state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * not for synchronization control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return {@code true} if any thread holds this lock and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public boolean isLocked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        return sync.isLocked();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Returns {@code true} if this lock has fairness set true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @return {@code true} if this lock has fairness set true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public final boolean isFair() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return sync instanceof FairSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Returns the thread that currently owns this lock, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * {@code null} if not owned. When this method is called by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * thread that is not the owner, the return value reflects a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * best-effort approximation of current lock status. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * the owner may be momentarily {@code null} even if there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * threads trying to acquire the lock but have not yet done so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * This method is designed to facilitate construction of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * subclasses that provide more extensive lock monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return the owner, or {@code null} if not owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    protected Thread getOwner() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return sync.getOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Queries whether any threads are waiting to acquire this lock. Note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * because cancellations may occur at any time, a {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * return does not guarantee that any other thread will ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * acquire this lock.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return {@code true} if there may be other threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *         acquire the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    public final boolean hasQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return sync.hasQueuedThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Queries whether the given thread is waiting to acquire this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * lock. Note that because cancellations may occur at any time, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * {@code true} return does not guarantee that this thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * will ever acquire this lock.  This method is designed primarily for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * in monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @param thread the thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @return {@code true} if the given thread is queued waiting for this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @throws NullPointerException if the thread is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    public final boolean hasQueuedThread(Thread thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return sync.isQueued(thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Returns an estimate of the number of threads waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * acquire this lock.  The value is only an estimate because the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * threads may change dynamically while this method traverses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * internal data structures.  This method is designed for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * monitoring of the system state, not for synchronization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return the estimated number of threads waiting for this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public final int getQueueLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        return sync.getQueueLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Returns a collection containing threads that may be waiting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * acquire this lock.  Because the actual set of threads may change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * dynamically while constructing this result, the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * collection is only a best-effort estimate.  The elements of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * returned collection are in no particular order.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * designed to facilitate construction of subclasses that provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * more extensive monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    protected Collection<Thread> getQueuedThreads() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return sync.getQueuedThreads();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Queries whether any threads are waiting on the given condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * associated with this lock. Note that because timeouts and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * interrupts may occur at any time, a {@code true} return does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * not guarantee that a future {@code signal} will awaken any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * threads.  This method is designed primarily for use in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * monitoring of the system state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @return {@code true} if there are any waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @throws IllegalMonitorStateException if this lock is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *         not associated with this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public boolean hasWaiters(Condition condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (condition == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            throw new IllegalArgumentException("not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return sync.hasWaiters((AbstractQueuedSynchronizer.ConditionObject)condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Returns an estimate of the number of threads waiting on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * given condition associated with this lock. Note that because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * timeouts and interrupts may occur at any time, the estimate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * serves only as an upper bound on the actual number of waiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * This method is designed for use in monitoring of the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * state, not for synchronization control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @return the estimated number of waiting threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @throws IllegalMonitorStateException if this lock is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *         not associated with this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    public int getWaitQueueLength(Condition condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (condition == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            throw new IllegalArgumentException("not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        return sync.getWaitQueueLength((AbstractQueuedSynchronizer.ConditionObject)condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * Returns a collection containing those threads that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * waiting on the given condition associated with this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Because the actual set of threads may change dynamically while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * constructing this result, the returned collection is only a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * best-effort estimate. The elements of the returned collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * are in no particular order.  This method is designed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * facilitate construction of subclasses that provide more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * extensive condition monitoring facilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @param condition the condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @return the collection of threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @throws IllegalMonitorStateException if this lock is not held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @throws IllegalArgumentException if the given condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *         not associated with this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @throws NullPointerException if the condition is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    protected Collection<Thread> getWaitingThreads(Condition condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        if (condition == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        if (!(condition instanceof AbstractQueuedSynchronizer.ConditionObject))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            throw new IllegalArgumentException("not owner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return sync.getWaitingThreads((AbstractQueuedSynchronizer.ConditionObject)condition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * Returns a string identifying this lock, as well as its lock state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * The state, in brackets, includes either the String {@code "Unlocked"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * or the String {@code "Locked by"} followed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * {@linkplain Thread#getName name} of the owning thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @return a string identifying this lock, as well as its lock state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        Thread o = sync.getOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return super.toString() + ((o == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                                   "[Unlocked]" :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                   "[Locked by thread " + o.getName() + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
}