jdk/src/share/classes/java/util/concurrent/locks/Lock.java
author dl
Wed, 12 Jan 2011 14:40:36 +0000
changeset 7976 f273c0d04215
parent 5506 202f599c92aa
child 9242 ef138d47df58
permissions -rw-r--r--
7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011 Reviewed-by: dholmes, chegar, mduigou
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.concurrent.TimeUnit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * {@code Lock} implementations provide more extensive locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * operations than can be obtained using {@code synchronized} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * and statements.  They allow more flexible structuring, may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * quite different properties, and may support multiple associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@link Condition} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>A lock is a tool for controlling access to a shared resource by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * multiple threads. Commonly, a lock provides exclusive access to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * shared resource: only one thread at a time can acquire the lock and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * all access to the shared resource requires that the lock be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * acquired first. However, some locks may allow concurrent access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * a shared resource, such as the read lock of a {@link ReadWriteLock}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>The use of {@code synchronized} methods or statements provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * access to the implicit monitor lock associated with every object, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * forces all lock acquisition and release to occur in a block-structured way:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * when multiple locks are acquired they must be released in the opposite
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * order, and all locks must be released in the same lexical scope in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * they were acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>While the scoping mechanism for {@code synchronized} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * and statements makes it much easier to program with monitor locks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and helps avoid many common programming errors involving locks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * there are occasions where you need to work with locks in a more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * flexible way. For example, some algorithms for traversing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * concurrently accessed data structures require the use of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * &quot;hand-over-hand&quot; or &quot;chain locking&quot;: you
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * acquire the lock of node A, then node B, then release A and acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * C, then release B and acquire D and so on.  Implementations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * {@code Lock} interface enable the use of such techniques by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * allowing a lock to be acquired and released in different scopes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * and allowing multiple locks to be acquired and released in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>With this increased flexibility comes additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * responsibility. The absence of block-structured locking removes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * automatic release of locks that occurs with {@code synchronized}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * methods and statements. In most cases, the following idiom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * should be used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * <pre><tt>     Lock l = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     l.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         // access the resource protected by this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         l.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * </tt></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * When locking and unlocking occur in different scopes, care must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * taken to ensure that all code that is executed while the lock is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * held is protected by try-finally or try-catch to ensure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * lock is released when necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>{@code Lock} implementations provide additional functionality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * over the use of {@code synchronized} methods and statements by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * providing a non-blocking attempt to acquire a lock ({@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * #tryLock()}), an attempt to acquire the lock that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * interrupted ({@link #lockInterruptibly}, and an attempt to acquire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * the lock that can timeout ({@link #tryLock(long, TimeUnit)}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <p>A {@code Lock} class can also provide behavior and semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * that is quite different from that of the implicit monitor lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * such as guaranteed ordering, non-reentrant usage, or deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * detection. If an implementation provides such specialized semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * then the implementation must document those semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>Note that {@code Lock} instances are just normal objects and can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * themselves be used as the target in a {@code synchronized} statement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Acquiring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * monitor lock of a {@code Lock} instance has no specified relationship
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * with invoking any of the {@link #lock} methods of that instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * It is recommended that to avoid confusion you never use {@code Lock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * instances in this way, except within their own implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <p>Except where noted, passing a {@code null} value for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * parameter will result in a {@link NullPointerException} being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <h3>Memory Synchronization</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <p>All {@code Lock} implementations <em>must</em> enforce the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * memory synchronization semantics as provided by the built-in monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * lock, as described in <a href="http://java.sun.com/docs/books/jls/">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * The Java Language Specification, Third Edition (17.4 Memory Model)</a>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <li>A successful {@code lock} operation has the same memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * synchronization effects as a successful <em>Lock</em> action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <li>A successful {@code unlock} operation has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * memory synchronization effects as a successful <em>Unlock</em> action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * Unsuccessful locking and unlocking operations, and reentrant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * locking/unlocking operations, do not require any memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * synchronization effects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <h3>Implementation Considerations</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p> The three forms of lock acquisition (interruptible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * non-interruptible, and timed) may differ in their performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * characteristics, ordering guarantees, or other implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * qualities.  Further, the ability to interrupt the <em>ongoing</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * acquisition of a lock may not be available in a given {@code Lock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * class.  Consequently, an implementation is not required to define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * exactly the same guarantees or semantics for all three forms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * lock acquisition, nor is it required to support interruption of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * ongoing lock acquisition.  An implementation is required to clearly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * document the semantics and guarantees provided by each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * locking methods. It must also obey the interruption semantics as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * defined in this interface, to the extent that interruption of lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * acquisition is supported: which is either totally, or only on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * method entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p>As interruption generally implies cancellation, and checks for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * interruption are often infrequent, an implementation can favor responding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * to an interrupt over normal method return. This is true even if it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * shown that the interrupt occurred after another action may have unblocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * the thread. An implementation should document this behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * @see ReentrantLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * @see Condition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * @see ReadWriteLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
public interface Lock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Acquires the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>If the lock is not available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * disabled for thread scheduling purposes and lies dormant until the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * lock has been acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p>A {@code Lock} implementation may be able to detect erroneous use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * of the lock, such as an invocation that would cause deadlock, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * may throw an (unchecked) exception in such circumstances.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * circumstances and the exception type must be documented by that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * {@code Lock} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    void lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Acquires the lock unless the current thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>Acquires the lock if it is available and returns immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p>If the lock is not available then the current thread becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * disabled for thread scheduling purposes and lies dormant until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * one of two things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <li>The lock is acquired by the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * current thread, and interruption of lock acquisition is supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * lock, and interruption of lock acquisition is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>The ability to interrupt a lock acquisition in some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * implementations may not be possible, and if possible may be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * expensive operation.  The programmer should be aware that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * may be the case. An implementation should document when this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>An implementation can favor responding to an interrupt over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * normal method return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <p>A {@code Lock} implementation may be able to detect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * erroneous use of the lock, such as an invocation that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * cause deadlock, and may throw an (unchecked) exception in such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * circumstances.  The circumstances and the exception type must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * be documented by that {@code Lock} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @throws InterruptedException if the current thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *         interrupted while acquiring the lock (and interruption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *         of lock acquisition is supported).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    void lockInterruptibly() throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Acquires the lock only if it is free at the time of invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>Acquires the lock if it is available and returns immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * with the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * If the lock is not available then this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * immediately with the value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <p>A typical usage idiom for this method would be:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *      Lock lock = ...;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *      if (lock.tryLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *          try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *              // manipulate protected state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *          } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *              lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *          // perform alternative actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * This usage ensures that the lock is unlocked if it was acquired, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * doesn't try to unlock if the lock was not acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return {@code true} if the lock was acquired and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    boolean tryLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Acquires the lock if it is free within the given waiting time and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * current thread has not been {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * <p>If the lock is available this method returns immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * with the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * If the lock is not available then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * purposes and lies dormant until one of three things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <li>The lock is acquired by the current thread; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * current thread, and interruption of lock acquisition is supported; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <li>The specified waiting time elapses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>If the lock is acquired then the value {@code true} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <li>is {@linkplain Thread#interrupt interrupted} while acquiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * the lock, and interruption of lock acquisition is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * interrupted status is cleared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>If the specified waiting time elapses then the value {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * If the time is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * less than or equal to zero, the method will not wait at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p>The ability to interrupt a lock acquisition in some implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * may not be possible, and if possible may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * be an expensive operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * The programmer should be aware that this may be the case. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * implementation should document when this is the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>An implementation can favor responding to an interrupt over normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * method return, or reporting a timeout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <p>A {@code Lock} implementation may be able to detect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * erroneous use of the lock, such as an invocation that would cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * deadlock, and may throw an (unchecked) exception in such circumstances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * The circumstances and the exception type must be documented by that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * {@code Lock} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param time the maximum time to wait for the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param unit the time unit of the {@code time} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return {@code true} if the lock was acquired and {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *         if the waiting time elapsed before the lock was acquired
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         while acquiring the lock (and interruption of lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         acquisition is supported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Releases the lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>A {@code Lock} implementation will usually impose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * restrictions on which thread can release a lock (typically only the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * holder of the lock can release it) and may throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * an (unchecked) exception if the restriction is violated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Any restrictions and the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * type must be documented by that {@code Lock} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    void unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Returns a new {@link Condition} instance that is bound to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * {@code Lock} instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <p>Before waiting on the condition the lock must be held by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * A call to {@link Condition#await()} will atomically release the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * before waiting and re-acquire the lock before the wait returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <p>The exact operation of the {@link Condition} instance depends on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * the {@code Lock} implementation and must be documented by that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @return A new {@link Condition} instance for this {@code Lock} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @throws UnsupportedOperationException if this {@code Lock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         implementation does not support conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    Condition newCondition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}