src/java.base/share/classes/java/util/concurrent/locks/Condition.java
author dl
Thu, 02 May 2019 06:33:28 -0700
changeset 54685 e1bec7613945
parent 50764 5637aca18f1d
permissions -rw-r--r--
8220248: fix headings in java.util.concurrent Reviewed-by: martin, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7976
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent.locks;
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    37
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
    38
import java.util.Date;
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    39
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@code Condition} factors out the {@code Object} monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * methods ({@link Object#wait() wait}, {@link Object#notify notify}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * and {@link Object#notifyAll notifyAll}) into distinct objects to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * give the effect of having multiple wait-sets per object, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * combining them with the use of arbitrary {@link Lock} implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Where a {@code Lock} replaces the use of {@code synchronized} methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * and statements, a {@code Condition} replaces the use of the Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * monitor methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>Conditions (also known as <em>condition queues</em> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <em>condition variables</em>) provide a means for one thread to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * suspend execution (to &quot;wait&quot;) until notified by another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * thread that some state condition may now be true.  Because access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * to this shared state information occurs in different threads, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * must be protected, so a lock of some form is associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * condition. The key property that waiting for a condition provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * is that it <em>atomically</em> releases the associated lock and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * suspends the current thread, just like {@code Object.wait}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p>A {@code Condition} instance is intrinsically bound to a lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * To obtain a {@code Condition} instance for a particular {@link Lock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * instance use its {@link Lock#newCondition newCondition()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>As an example, suppose we have a bounded buffer which supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * {@code put} and {@code take} methods.  If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * {@code take} is attempted on an empty buffer, then the thread will block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * until an item becomes available; if a {@code put} is attempted on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * full buffer, then the thread will block until a space becomes available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * We would like to keep waiting {@code put} threads and {@code take}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * threads in separate wait-sets so that we can use the optimization of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * only notifying a single thread at a time when items or spaces become
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * available in the buffer. This can be achieved using two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * {@link Condition} instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <pre>
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
    76
 * class BoundedBuffer&lt;E&gt; {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   <b>final Lock lock = new ReentrantLock();</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   final Condition notFull  = <b>lock.newCondition(); </b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   final Condition notEmpty = <b>lock.newCondition(); </b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   final Object[] items = new Object[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   int putptr, takeptr, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
    84
 *   public void put(E x) throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     <b>lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     try {</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *       while (count == items.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *         <b>notFull.await();</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *       items[putptr] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *       if (++putptr == items.length) putptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *       ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *       <b>notEmpty.signal();</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     <b>} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *       lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     }</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
    98
 *   public E take() throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     <b>lock.lock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     try {</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *       while (count == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *         <b>notEmpty.await();</b>
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   103
 *       E x = (E) items[takeptr];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *       if (++takeptr == items.length) takeptr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *       --count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *       <b>notFull.signal();</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *       return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *     <b>} finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *       lock.unlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *     }</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * (The {@link java.util.concurrent.ArrayBlockingQueue} class provides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * this functionality, so there is no reason to implement this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * sample usage class.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p>A {@code Condition} implementation can provide behavior and semantics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * different from that of the {@code Object} monitor methods, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * guaranteed ordering for notifications, or not requiring a lock to be held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * when performing notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * If an implementation provides such specialized semantics then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * implementation must document those semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>Note that {@code Condition} instances are just normal objects and can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * themselves be used as the target in a {@code synchronized} statement,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * and can have their own monitor {@link Object#wait wait} and
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   130
 * {@link Object#notify notify} methods invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Acquiring the monitor lock of a {@code Condition} instance, or using its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * monitor methods, has no specified relationship with acquiring the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * {@link Lock} associated with that {@code Condition} or the use of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * {@linkplain #await waiting} and {@linkplain #signal signalling} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * It is recommended that to avoid confusion you never use {@code Condition}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * instances in this way, except perhaps within their own implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p>Except where noted, passing a {@code null} value for any parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * will result in a {@link NullPointerException} being thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
54685
e1bec7613945 8220248: fix headings in java.util.concurrent
dl
parents: 50764
diff changeset
   141
 * <h2>Implementation Considerations</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <p>When waiting upon a {@code Condition}, a &quot;<em>spurious
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * wakeup</em>&quot; is permitted to occur, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * general, as a concession to the underlying platform semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * This has little practical impact on most application programs as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * {@code Condition} should always be waited upon in a loop, testing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * the state predicate that is being waited for.  An implementation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * free to remove the possibility of spurious wakeups but it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * recommended that applications programmers always assume that they can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * occur and so always wait in a loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p>The three forms of condition waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * (interruptible, non-interruptible, and timed) may differ in their ease of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * implementation on some platforms and in their performance characteristics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * In particular, it may be difficult to provide these features and maintain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * specific semantics such as ordering guarantees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * Further, the ability to interrupt the actual suspension of the thread may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * not always be feasible to implement on all platforms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * <p>Consequently, an implementation is not required to define exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * same guarantees or semantics for all three forms of waiting, nor is it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * required to support interruption of the actual suspension of the thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * <p>An implementation is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * clearly document the semantics and guarantees provided by each of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * waiting methods, and when an implementation does support interruption of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * thread suspension then it must obey the interruption semantics as defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * in this interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <p>As interruption generally implies cancellation, and checks for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * interruption are often infrequent, an implementation can favor responding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * to an interrupt over normal method return. This is true even if it can be
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   174
 * shown that the interrupt occurred after another action that may have
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 2
diff changeset
   175
 * unblocked the thread. An implementation should document this behavior.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
public interface Condition {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Causes the current thread to wait until it is signalled or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * {@linkplain Thread#interrupt interrupted}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p>The lock associated with this {@code Condition} is atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * released and the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * purposes and lies dormant until <em>one</em> of four things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <li>Some other thread invokes the {@link #signal} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * {@code Condition} and the current thread happens to be chosen as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * thread to be awakened; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <li>Some other thread invokes the {@link #signalAll} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * {@code Condition}; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * current thread, and interruption of thread suspension is supported; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <li>A &quot;<em>spurious wakeup</em>&quot; occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>In all cases, before this method can return the current thread must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * re-acquire the lock associated with this condition. When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * thread returns it is <em>guaranteed</em> to hold this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * and interruption of thread suspension is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * interrupted status is cleared. It is not specified, in the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * case, whether or not the test for interruption occurs before the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * is released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <p>The current thread is assumed to hold the lock associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * {@code Condition} when this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * It is up to the implementation to determine if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the case and if not, how to respond. Typically, an exception will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * thrown (such as {@link IllegalMonitorStateException}) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * implementation must document that fact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <p>An implementation can favor responding to an interrupt over normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * method return in response to a signal. In that case the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * must ensure that the signal is redirected to another waiting thread, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *         (and interruption of thread suspension is supported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    void await() throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Causes the current thread to wait until it is signalled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <p>The lock associated with this condition is atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * released and the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * purposes and lies dormant until <em>one</em> of three things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <li>Some other thread invokes the {@link #signal} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * {@code Condition} and the current thread happens to be chosen as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * thread to be awakened; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <li>Some other thread invokes the {@link #signalAll} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * {@code Condition}; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <li>A &quot;<em>spurious wakeup</em>&quot; occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p>In all cases, before this method can return the current thread must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * re-acquire the lock associated with this condition. When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * thread returns it is <em>guaranteed</em> to hold this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p>If the current thread's interrupted status is set when it enters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * this method, or it is {@linkplain Thread#interrupt interrupted}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * while waiting, it will continue to wait until signalled. When it finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * returns from this method its interrupted status will still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>The current thread is assumed to hold the lock associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * {@code Condition} when this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * It is up to the implementation to determine if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * the case and if not, how to respond. Typically, an exception will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * thrown (such as {@link IllegalMonitorStateException}) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * implementation must document that fact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void awaitUninterruptibly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Causes the current thread to wait until it is signalled or interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * or the specified waiting time elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <p>The lock associated with this condition is atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * released and the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * purposes and lies dormant until <em>one</em> of five things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <li>Some other thread invokes the {@link #signal} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * {@code Condition} and the current thread happens to be chosen as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * thread to be awakened; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <li>Some other thread invokes the {@link #signalAll} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * {@code Condition}; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * current thread, and interruption of thread suspension is supported; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <li>The specified waiting time elapses; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <li>A &quot;<em>spurious wakeup</em>&quot; occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>In all cases, before this method can return the current thread must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * re-acquire the lock associated with this condition. When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * thread returns it is <em>guaranteed</em> to hold this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * and interruption of thread suspension is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * interrupted status is cleared. It is not specified, in the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * case, whether or not the test for interruption occurs before the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * is released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p>The method returns an estimate of the number of nanoseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * remaining to wait given the supplied {@code nanosTimeout}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * value upon return, or a value less than or equal to zero if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * timed out. This value can be used to determine whether and how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * long to re-wait in cases where the wait returns but an awaited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * condition still does not hold. Typical uses of this method take
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * the following form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   312
     * <pre> {@code
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   313
     * boolean aMethod(long timeout, TimeUnit unit)
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   314
     *     throws InterruptedException {
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 47307
diff changeset
   315
     *   long nanosRemaining = unit.toNanos(timeout);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   316
     *   lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   317
     *   try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   318
     *     while (!conditionBeingWaitedFor()) {
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 47307
diff changeset
   319
     *       if (nanosRemaining <= 0L)
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   320
     *         return false;
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 47307
diff changeset
   321
     *       nanosRemaining = theCondition.awaitNanos(nanosRemaining);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   322
     *     }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   323
     *     // ...
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   324
     *     return true;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   325
     *   } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   326
     *     lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *   }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   328
     * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
18576
7a5c231327af 8019377: Sync j.u.c locks and atomic from 166 to tl
dl
parents: 14325
diff changeset
   330
     * <p>Design note: This method requires a nanosecond argument so
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * as to avoid truncation errors in reporting remaining times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Such precision loss would make it difficult for programmers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * ensure that total waiting times are not systematically shorter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * than specified when re-waits occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>The current thread is assumed to hold the lock associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * {@code Condition} when this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * It is up to the implementation to determine if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * the case and if not, how to respond. Typically, an exception will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * thrown (such as {@link IllegalMonitorStateException}) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * implementation must document that fact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>An implementation can favor responding to an interrupt over normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * method return in response to a signal, or over indicating the elapse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * of the specified waiting time. In either case the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * must ensure that the signal is redirected to another waiting thread, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @param nanosTimeout the maximum time to wait, in nanoseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @return an estimate of the {@code nanosTimeout} value minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         the time spent waiting upon return from this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *         A positive value may be used as the argument to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *         subsequent call to this method to finish waiting out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *         the desired time.  A value less than or equal to zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         indicates that no time remains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *         (and interruption of thread suspension is supported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    long awaitNanos(long nanosTimeout) throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Causes the current thread to wait until it is signalled or interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * or the specified waiting time elapses. This method is behaviorally
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   366
     * equivalent to:
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   367
     * <pre> {@code awaitNanos(unit.toNanos(time)) > 0}</pre>
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
   368
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @param time the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @param unit the time unit of the {@code time} argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @return {@code false} if the waiting time detectably elapsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *         before return from the method, else {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *         (and interruption of thread suspension is supported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    boolean await(long time, TimeUnit unit) throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Causes the current thread to wait until it is signalled or interrupted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * or the specified deadline elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p>The lock associated with this condition is atomically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * released and the current thread becomes disabled for thread scheduling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * purposes and lies dormant until <em>one</em> of five things happens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <li>Some other thread invokes the {@link #signal} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * {@code Condition} and the current thread happens to be chosen as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * thread to be awakened; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * <li>Some other thread invokes the {@link #signalAll} method for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * {@code Condition}; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <li>Some other thread {@linkplain Thread#interrupt interrupts} the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * current thread, and interruption of thread suspension is supported; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <li>The specified deadline elapses; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <li>A &quot;<em>spurious wakeup</em>&quot; occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>In all cases, before this method can return the current thread must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * re-acquire the lock associated with this condition. When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * thread returns it is <em>guaranteed</em> to hold this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <p>If the current thread:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * <li>has its interrupted status set on entry to this method; or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <li>is {@linkplain Thread#interrupt interrupted} while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * and interruption of thread suspension is supported,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * then {@link InterruptedException} is thrown and the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * interrupted status is cleared. It is not specified, in the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * case, whether or not the test for interruption occurs before the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * is released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <p>The return value indicates whether the deadline has elapsed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * which can be used as follows:
32990
299a81977f48 8134855: Bulk integration of java.util.concurrent.locks classes
dl
parents: 25859
diff changeset
   414
     * <pre> {@code
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   415
     * boolean aMethod(Date deadline)
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   416
     *     throws InterruptedException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *   boolean stillWaiting = true;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   418
     *   lock.lock();
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   419
     *   try {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   420
     *     while (!conditionBeingWaitedFor()) {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   421
     *       if (!stillWaiting)
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   422
     *         return false;
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   423
     *       stillWaiting = theCondition.awaitUntil(deadline);
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   424
     *     }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   425
     *     // ...
47307
6864969a78ad 8186056: Miscellaneous changes imported from jsr166 CVS 2017-09
dl
parents: 47216
diff changeset
   426
     *     return true;
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   427
     *   } finally {
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   428
     *     lock.unlock();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *   }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   430
     * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p><b>Implementation Considerations</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p>The current thread is assumed to hold the lock associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * {@code Condition} when this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * It is up to the implementation to determine if this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * the case and if not, how to respond. Typically, an exception will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * thrown (such as {@link IllegalMonitorStateException}) and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * implementation must document that fact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <p>An implementation can favor responding to an interrupt over normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * method return in response to a signal, or over indicating the passing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * of the specified deadline. In either case the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * must ensure that the signal is redirected to another waiting thread, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @param deadline the absolute time to wait until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @return {@code false} if the deadline has elapsed upon return, else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *         {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @throws InterruptedException if the current thread is interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *         (and interruption of thread suspension is supported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    boolean awaitUntil(Date deadline) throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Wakes up one waiting thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * <p>If any threads are waiting on this condition then one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * is selected for waking up. That thread must then re-acquire the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * lock before returning from {@code await}.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   461
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   462
     * <p><b>Implementation Considerations</b>
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   463
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   464
     * <p>An implementation may (and typically does) require that the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   465
     * current thread hold the lock associated with this {@code
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   466
     * Condition} when this method is called. Implementations must
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   467
     * document this precondition and any actions taken if the lock is
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   468
     * not held. Typically, an exception such as {@link
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   469
     * IllegalMonitorStateException} will be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    void signal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Wakes up all waiting threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * <p>If any threads are waiting on this condition then they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * all woken up. Each thread must re-acquire the lock before it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * return from {@code await}.
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   479
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   480
     * <p><b>Implementation Considerations</b>
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   481
     *
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   482
     * <p>An implementation may (and typically does) require that the
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   483
     * current thread hold the lock associated with this {@code
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   484
     * Condition} when this method is called. Implementations must
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   485
     * document this precondition and any actions taken if the lock is
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   486
     * not held. Typically, an exception such as {@link
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
   487
     * IllegalMonitorStateException} will be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    void signalAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
}