版本
JDK8(JDK1.8)
Condition 接口源码重点
1.Condition将Object监视方法(Object.wait()、Object.notify() 和Object.notifyAll())分解出来,通过将它们与任意Lock实现相结合,使每个对象具有多个等待集的效果。当Lock替换了synchronized方法和语句时,Condition替换了对象监视器方法的使用。
Lock源码可以看我这篇文章 Lock
2.Condition(也称为条件队列或条件变量)为一个线程提供了一种暂停执行(即等待)的方法, 直到另一个线程通知。Condition提供的关键属性是, 它以原子方式释放关联的锁并挂起当前线程,就像Object.wait()
3.Condition实例本质上绑定到锁。要获取特定Lock实例的Condition实例,请使用其Lock.newCondition() 方法。
4.Condition中定义的所有方法使用之前都必须获取锁,否则会抛出 IllegalMonitorStateException 异常
5.Condition接口中定义的方法
方法名 | 作用 |
---|---|
void await() | 使当前线程等待,直到发出信号(即signal()或signalAll())或Thread.interrupt() |
void awaitUninterruptibly() | 使当前线程等待,直到发出信号。(无法被Thread.interrupt()中断) |
long awaitNanos(long nanosTimeout) | 使当前线程等待,直到发出信号或被中断,或经过指定的等待时间(时间单位:纳秒) |
await(long time, TimeUnit unit) | 使当前线程等待,直到发出信号或被中断,或经过指定的等待时间(时间单位:可自己指定) |
boolean awaitUntil(Date deadline) | 使当前线程等待,直到发出信号或被中断,或者指定的截止日期过去 |
void signal() | 唤醒一个正在等待的线程 |
void signalAll() | 唤醒所有等待的线程 |
Condition 接口源码
package java.util.concurrent.locks;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* {@code Condition} factors out the {@code Object} monitor
* methods ({@link Object#wait() wait}, {@link Object#notify notify}
* and {@link Object#notifyAll notifyAll}) into distinct objects to
* give the effect of having multiple wait-sets per object, by
* combining them with the use of arbitrary {@link Lock} implementations.
* Where a {@code Lock} replaces the use of {@code synchronized} methods
* and statements, a {@code Condition} replaces the use of the Object
* monitor methods.
* Condition将Object监视方法(Object.wait()、Object.notify()
* 和Object.notifyAll())分解成不同的对象,
* 通过将它们与任意Lock实现相结合,使每个对象具有多个等待集的效果。
* 当Lock替换了synchronized方法和语句时,Condition替换了对象监视器方法的使用。
*
*
*
*
*
*
*
* <p>Conditions (also known as <em>condition queues</em> or
* <em>condition variables</em>) provide a means for one thread to
* suspend execution (to "wait") until notified by another
* thread that some state condition may now be true. Because access
* to this shared state information occurs in different threads, it
* must be protected, so a lock of some form is associated with the
* condition. The key property that waiting for a condition provides
* is that it <em>atomically</em> releases the associated lock and
* suspends the current thread, just like {@code Object.wait}.
* 条件(也称为条件队列或条件变量)为一个线程提供了一种暂停执行(等待)的方法,
* 直到另一个线程通知某个状态条件现在可能为真。
* 由于对该共享状态信息的访问发生在不同的线程中,因此必须对其进行保护,
* 因此某种形式的锁与该条件相关联。等待条件提供的关键属性是,
* 它以原子方式释放关联的锁并挂起当前线程,就像Object.wait()
*
*
*
*
*
*
*
* <p>A {@code Condition} instance is intrinsically bound to a lock.
* To obtain a {@code Condition} instance for a particular {@link Lock}
* instance use its {@link Lock#newCondition newCondition()} method.
* Condition实例本质上绑定到锁。要获取特定Lock实例的Condition实例,
* 请使用其Lock.newCondition()方法。
*
*
*
*
*
*
* <p>As an example, suppose we have a bounded buffer which supports
* {@code put} and {@code take} methods. If a
* {@code take} is attempted on an empty buffer, then the thread will block
* until an item becomes available; if a {@code put} is attempted on a
* full buffer, then the thread will block until a space becomes available.
* We would like to keep waiting {@code put} threads and {@code take}
* threads in separate wait-sets so that we can use the optimization of
* only notifying a single thread at a time when items or spaces become
* available in the buffer. This can be achieved using two
* {@link Condition} instances.
* 例如,假设我们有一个支持put和take方法的有界缓冲区。
* 如果在空缓冲区上尝试take,则线程将阻塞,直到某个项可用;
* 如果试图在已满的缓冲区上执行put,则线程将阻塞,直到有可用空间为止。
* 我们希望在单独的等待集中继续等待put线程和take线程,
* 以便在缓冲区中的项目或空间可用时,我们可以使用只通知单个线程的优化。
* 这可以通过使用两个Condition实例来实现。
*
*
*
*
*
* <pre>
* class BoundedBuffer<E> {
* final Lock lock = new ReentrantLock();
* final Condition notFull = lock.newCondition();
* final Condition notEmpty = lock.newCondition();
*
* final Object[] items = new Object[100];
* int putptr, takeptr, count;
*
* public void put(E x) throws InterruptedException {
* lock.lock();
* try {
* while (count == items.length)
* notFull.await();
* items[putptr] = x;
* if (++putptr == items.length) putptr = 0;
* ++count;
* notEmpty.signal();
* } finally {
* lock.unlock();
* }
* }
*
* public E take() throws InterruptedException {
* lock.lock();
* try {
* while (count == 0)
* notEmpty.await();
* E x = (E) items[takeptr];
* if (++takeptr == items.length) takeptr = 0;
* --count;
* notFull.signal();
* return x;
* } finally {
* lock.unlock();
* }
* }
* }
* </pre>
*
* (The {@link java.util.concurrent.ArrayBlockingQueue} class provides
* this functionality, so there is no reason to implement this
* sample usage class.)
* java.util.concurrent.ArrayBlockingQueue 类提供了此功能,因此没有理由实现此示例使用类。
*
*
*
* <p>A {@code Condition} implementation can provide behavior and semantics
* that is
* different from that of the {@code Object} monitor methods, such as
* guaranteed ordering for notifications, or not requiring a lock to be held
* when performing notifications.
* If an implementation provides such specialized semantics then the
* implementation must document those semantics.
* Condition实现可以提供不同于Object监视方法的行为和语义,
* 例如保证通知的顺序,或者在执行通知时不需要持有锁。
* 如果一个实现提供了这种专门的语义,那么该实现必须记录这些语义。
*
*
*
* <p>Note that {@code Condition} instances are just normal objects and can
* themselves be used as the target in a {@code synchronized} statement,
* and can have their own monitor {@link Object#wait wait} and
* {@link Object#notify notify} methods invoked.
* 请注意,Condition实例只是普通对象,它们本身可以用作synchronized
* 语句中的目标,并且可以调用它们自己的监视器Object.wait()和Object.notify()方法。
*
*
* Acquiring the monitor lock of a {@code Condition} instance, or using its
* monitor methods, has no specified relationship with acquiring the
* {@link Lock} associated with that {@code Condition} or the use of its
* {@linkplain #await waiting} and {@linkplain #signal signalling} methods.
* It is recommended that to avoid confusion you never use {@code Condition}
* instances in this way, except perhaps within their own implementation.
* 获取Condition实例的监视器锁或使用其监视器方法与获取
* 与该Condition关联的lock或使用其wait()和signal()方法没有特定的关系。
* 为了避免混淆,建议不要以这种方式使用Condition实例,可能在它们自己的实现中除外。
*
*
*
*
* <p>Except where noted, passing a {@code null} value for any parameter
* will result in a {@link NullPointerException} being thrown.
* 除非另有说明,否则为任何参数传递null值将导致抛出NullPointerException。
*
*
* <h3>Implementation Considerations</h3>
* 关于实现的一些考虑
*
*
* <p>When waiting upon a {@code Condition}, a "<em>spurious
* wakeup</em>" is permitted to occur, in
* general, as a concession to the underlying platform semantics.
* This has little practical impact on most application programs as a
* {@code Condition} should always be waited upon in a loop, testing
* the state predicate that is being waited for. An implementation is
* free to remove the possibility of spurious wakeups but it is
* recommended that applications programmers always assume that they can
* occur and so always wait in a loop.
* 在等待 Condition时,通常允许出现虚假唤醒,作为对底层平台语义的让步。
* 这对大多数应用程序几乎没有实际影响,因为Condition应该始终在循环中等待,
* 测试正在等待的状态谓词。实现可以自由地消除虚假唤醒的可能性,
* 但建议应用程序程序员始终假设它们可以发生,因此始终在循环中等待。
*
*
*
* <p>The three forms of condition waiting
* (interruptible, non-interruptible, and timed) may differ in their ease of
* implementation on some platforms and in their performance characteristics.
* In particular, it may be difficult to provide these features and maintain
* specific semantics such as ordering guarantees.
* Further, the ability to interrupt the actual suspension of the thread may
* not always be feasible to implement on all platforms.
* 条件等待的三种形式(可中断、不可中断和定时)
* 在某些平台上的易实现性和性能特征上可能有所不同。
* 特别是,可能很难提供这些特性并维护特定的语义,例如排序保证。
* 此外,中断线程实际暂停的能力可能并不总是能够在所有平台上实现。
*
*
*
*
*
* <p>Consequently, an implementation is not required to define exactly the
* same guarantees or semantics for all three forms of waiting, nor is it
* required to support interruption of the actual suspension of the thread.
* 因此,实现不需要为所有三种形式的等待定义完全相同的保证或语义,
* 也不需要支持线程实际暂停的中断。
*
*
*
*
*
* <p>An implementation is required to
* clearly document the semantics and guarantees provided by each of the
* waiting methods, and when an implementation does support interruption of
* thread suspension then it must obey the interruption semantics as defined
* in this interface.
* 一个实现需要清楚地记录每个等待方法提供的语义和保证,
* 当一个实现确实支持线程挂起的中断时,它必须遵守这个接口中定义的中断语义。
*
*
*
*
*
* <p>As interruption generally implies cancellation, and checks for
* interruption are often infrequent, an implementation can favor responding
* to an interrupt over normal method return. This is true even if it can be
* shown that the interrupt occurred after another action that may have
* unblocked the thread. An implementation should document this behavior.
* 由于中断通常意味着取消,并且对中断的检查通常不经常发生,
* 所以实现可能更倾向于响应中断而不是正常的方法返回。
* 即使可以显示中断发生在另一个可能已解除线程阻塞的操作之后,
* 这也是正确的。实现应该记录这种行为。
*
* @since 1.5
* @author Doug Lea
*/
public interface Condition {
/**
* Causes the current thread to wait until it is signalled or
* {@linkplain Thread#interrupt interrupted}.
* 使当前线程等待,直到发出信号或Thread.interrupt()
*
*
*
*
* <p>The lock associated with this {@code Condition} is atomically
* released and the current thread becomes disabled for thread scheduling
* purposes and lies dormant until <em>one</em> of four things happens:
* 与此Conditio关联的锁被自动释放,当前线程出于线程调度目的被禁用,
* 并处于休眠状态,直到发生四种情况中的一种:
*
*
* <ul>
* <li>Some other thread invokes the {@link #signal} method for this
* {@code Condition} and the current thread happens to be chosen as the
* thread to be awakened; or
* 其他一些线程为此Condition调用signal()方法,
* 而当前线程恰好被选为要唤醒的线程;或
*
* <li>Some other thread invokes the {@link #signalAll} method for this
* {@code Condition}; or
* 其他一些线程为此Condition调用signalAll方法;或
*
*
* <li>Some other thread {@linkplain Thread#interrupt interrupts} the
* current thread, and interruption of thread suspension is supported; or
* 其他一些线程Thread.interrupt()当前线程,支持中断线程挂起;或
*
*
* <li>A "<em>spurious wakeup</em>" occurs.
* 出现虚假唤醒
* </ul>
*
* <p>In all cases, before this method can return the current thread must
* re-acquire the lock associated with this condition. When the
* thread returns it is <em>guaranteed</em> to hold this lock.
* 在所有情况下,在该方法返回之前,
* 当前线程必须重新获取与此条件关联的锁。当线程返回时,保证持有该锁。
*
*
* <p>If the current thread:
* 如果当前线程
*
* <ul>
* <li>has its interrupted status set on entry to this method; or
* 在进入该方法时设置其中断状态;或
*
* <li>is {@linkplain Thread#interrupt interrupted} while waiting
* and interruption of thread suspension is supported,
* 在等待时被调用Thread.interrupt(),支持中断线程挂起,
*
* </ul>
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared. It is not specified, in the first
* case, whether or not the test for interruption occurs before the lock
* is released.
* 然后抛出InterruptedException,并清除当前线程的中断状态。
* 在第一种情况下,未规定是否在释放锁之前进行中断测试。
*
*
* <p><b>Implementation Considerations</b>
* 关于实现的一些考虑
*
*
* <p>The current thread is assumed to hold the lock associated with this
* {@code Condition} when this method is called.
* It is up to the implementation to determine if this is
* the case and if not, how to respond. Typically, an exception will be
* thrown (such as {@link IllegalMonitorStateException}) and the
* implementation must document that fact.
* 调用此方法时,假定当前线程持有与此 Condition 关联的锁。
* 由实施部门确定是否存在这种情况,如果不是,如何应对。
* 通常,将引发异常(例如IllegalMonitorStateException),实现必须记录该事实。
*
*
*
* <p>An implementation can favor responding to an interrupt over normal
* method return in response to a signal. In that case the implementation
* must ensure that the signal is redirected to another waiting thread, if
* there is one.
* 与响应信号的正常方法返回相比,实现更倾向于响应中断。
* 在这种情况下,实现必须确保信号被重定向到另一个等待线程(如果有)。
*
*
*
* @throws InterruptedException if the current thread is interrupted
* (and interruption of thread suspension is supported)
* 如果当前线程被中断(并且支持中断线程挂起)
*/
void await() throws InterruptedException;
/**
* Causes the current thread to wait until it is signalled.
* 使当前线程等待,直到发出信号。(无法被Thread.interrupt()中断)
*
* <p>The lock associated with this condition is atomically
* released and the current thread becomes disabled for thread scheduling
* purposes and lies dormant until <em>one</em> of three things happens:
* 与此条件相关联的锁被自动释放,当前线程出于线程调度目的被禁用,
* 并处于休眠状态,直到发生以下三种情况之一:
*
* <ul>
* <li>Some other thread invokes the {@link #signal} method for this
* {@code Condition} and the current thread happens to be chosen as the
* thread to be awakened; or
* 其他一些线程为此Condition调用signal方法,
* 而当前线程恰好被选为要唤醒的线程;或
*
* <li>Some other thread invokes the {@link #signalAll} method for this
* {@code Condition}; or
* 其他一些线程为此Condition调用signalAll方法;或
*
*
* <li>A "<em>spurious wakeup</em>" occurs.
* 出现虚假唤醒
* </ul>
*
* <p>In all cases, before this method can return the current thread must
* re-acquire the lock associated with this condition. When the
* thread returns it is <em>guaranteed</em> to hold this lock.
* 在所有情况下,在该方法返回之前,
* 当前线程必须重新获取与此条件关联的锁。当线程返回时,保证持有该锁。
*
*
*
* <p>If the current thread's interrupted status is set when it enters
* this method, or it is {@linkplain Thread#interrupt interrupted}
* while waiting, it will continue to wait until signalled. When it finally
* returns from this method its interrupted status will still
* be set.
* 如果当前线程进入此方法时设置了中断状态,
* 或者在等待时为Thread.interrupt(),则它将继续等待,
* 直到发出信号。当它最终从此方法返回时,其中断状态仍将设置。
*
*
*
* <p><b>Implementation Considerations</b>
* 关于实现的一些考虑
*
*
*
* <p>The current thread is assumed to hold the lock associated with this
* {@code Condition} when this method is called.
* It is up to the implementation to determine if this is
* the case and if not, how to respond. Typically, an exception will be
* thrown (such as {@link IllegalMonitorStateException}) and the
* implementation must document that fact.
* 调用此方法时,假定当前线程持有与此Condition关联的锁。
* 由具体实现确定是否存在这种情况,如果不是,如何应对。
* 通常,将引发异常(例如IllegalMonitorStateException),实现必须记录该事实。
*
*
*/
void awaitUninterruptibly();
/**
* Causes the current thread to wait until it is signalled or interrupted,
* or the specified waiting time elapses.
* 使当前线程等待,直到发出信号或被中断,或经过指定的等待时间。
*
*
* <p>The lock associated with this condition is atomically
* released and the current thread becomes disabled for thread scheduling
* purposes and lies dormant until <em>one</em> of five things happens:
* 与此条件相关联的锁被自动释放,当前线程出于线程调度目的被禁用,
* 并处于休眠状态,直到发生五种情况中的一种:
*
*
* <ul>
* <li>Some other thread invokes the {@link #signal} method for this
* {@code Condition} and the current thread happens to be chosen as the
* thread to be awakened; or
* 其他一些线程为此Condition调用signal方法,而当前线程恰好被选为要唤醒的线程;或
*
*
* <li>Some other thread invokes the {@link #signalAll} method for this
* {@code Condition}; or
* 其他一些线程为此Condition调用signalAll方法;或
*
*
* <li>Some other thread {@linkplain Thread#interrupt interrupts} the
* current thread, and interruption of thread suspension is supported; or
* 其他一些线程对当前线程调用Thread.interrupt(),支持中断线程挂起;或
*
*
* <li>The specified waiting time elapses; or
* 经过指定的等待时间;或
*
* <li>A "<em>spurious wakeup</em>" occurs.
* 出现虚假唤醒
* </ul>
*
* <p>In all cases, before this method can return the current thread must
* re-acquire the lock associated with this condition. When the
* thread returns it is <em>guaranteed</em> to hold this lock.
* 在所有情况下,在该方法返回之前,
* 当前线程必须重新获取与此条件关联的锁。当线程返回时,保证持有此锁。
*
*
*
* <p>If the current thread:
* 如果当前线程:
*
* <ul>
* <li>has its interrupted status set on entry to this method; or
* 在进入该方法时设置其中断状态;或
*
* <li>is {@linkplain Thread#interrupt interrupted} while waiting
* and interruption of thread suspension is supported,
* 在等待时被调用Thread.interrupt(),支持中断线程挂起,
*
* </ul>
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared. It is not specified, in the first
* case, whether or not the test for interruption occurs before the lock
* is released.
* 然后抛出InterruptedException,并清除当前线程的中断状态。
* 在第一种情况下,未规定是否在释放锁之前进行中断测试。
*
*
*
* <p>The method returns an estimate of the number of nanoseconds
* remaining to wait given the supplied {@code nanosTimeout}
* value upon return, or a value less than or equal to zero if it
* timed out. This value can be used to determine whether and how
* long to re-wait in cases where the wait returns but an awaited
* condition still does not hold. Typical uses of this method take
* the following form:
* 该方法返回给定返回时提供的nanosTimeout值的剩余等待纳秒数的估计值,
* 或者如果超时,则返回小于或等于零的值。
* 此值可用于确定在等待返回但等待的条件仍然不成立的情况下是否重新等待以及等待多长时间。
* 该方法的典型用途如下:
*
* <pre> {@code
* boolean aMethod(long timeout, TimeUnit unit)
* throws InterruptedException {
* long nanosRemaining = unit.toNanos(timeout);
* lock.lock();
* try {
* while (!conditionBeingWaitedFor()) {
* if (nanosRemaining <= 0L)
* return false;
* nanosRemaining = theCondition.awaitNanos(nanosRemaining);
* }
* // ...
* return true;
* } finally {
* lock.unlock();
* }
* }}</pre>
*
* <p>Design note: This method requires a nanosecond argument so
* as to avoid truncation errors in reporting remaining times.
* Such precision loss would make it difficult for programmers to
* ensure that total waiting times are not systematically shorter
* than specified when re-waits occur.
* 设计说明:此方法需要纳秒参数,以避免报告剩余时间时出现截断错误。
* 这样的精度损失将使程序员难以确保在重新等待时总等待时间不会系统地短于指定的时间。
*
*
*
* <p><b>Implementation Considerations</b>
* 关于实现的一些考虑
*
*
*
* <p>The current thread is assumed to hold the lock associated with this
* {@code Condition} when this method is called.
* It is up to the implementation to determine if this is
* the case and if not, how to respond. Typically, an exception will be
* thrown (such as {@link IllegalMonitorStateException}) and the
* implementation must document that fact.
* 调用此方法时,假定当前线程持有与此 Condition关联的锁。
* 由具体确定是否存在这种情况,如果不是,如何应对。
* 通常,将引发异常(例如IllegalMonitorStateException),实现必须记录该事实。
*
*
*
*
* <p>An implementation can favor responding to an interrupt over normal
* method return in response to a signal, or over indicating the elapse
* of the specified waiting time. In either case the implementation
* must ensure that the signal is redirected to another waiting thread, if
* there is one.
* 一个实现可能有利于响应中断,而不是响应信号的正常方法返回,
* 或者过度指示指定等待时间的流逝。
* 在任何一种情况下,实现都必须确保信号被重定向到另一个等待线程(如果有)。
*
*
*
*
* @param nanosTimeout the maximum time to wait, in nanoseconds 等待的最长时间,以纳秒为单位
* @return an estimate of the {@code nanosTimeout} value minus
* the time spent waiting upon return from this method.
* A positive value may be used as the argument to a
* subsequent call to this method to finish waiting out
* the desired time. A value less than or equal to zero
* indicates that no time remains.
* nanosTimeout值减去等待此方法返回所花费的时间的估计值。
* 正值可以用作此方法后续调用的参数,以完成所需时间的等待。
* 小于或等于零的值表示没有剩余时间。
*
*
*
* @throws InterruptedException if the current thread is interrupted
* (and interruption of thread suspension is supported)
* 如果当前线程被中断(并且支持中断线程挂起)
*/
long awaitNanos(long nanosTimeout) throws InterruptedException;
/**
* Causes the current thread to wait until it is signalled or interrupted,
* or the specified waiting time elapses. This method is behaviorally
* equivalent to:
* 使当前线程等待,直到发出信号或被中断,
* 或经过指定的等待时间。此方法在行为上等同于:
*
* <pre> {@code awaitNanos(unit.toNanos(time)) > 0}</pre>
*
* @param time the maximum time to wait 等待的最长时间
* @param unit the time unit of the {@code time} argument
* time参数的时间单位
* @return {@code false} if the waiting time detectably elapsed
* before return from the method, else {@code true}
* 返回false,如果在从方法返回之前可检测到等待时间已过,则返回true
*
*
* @throws InterruptedException if the current thread is interrupted
* (and interruption of thread suspension is supported)
* 如果当前线程被中断(并且支持中断线程挂起)
*/
boolean await(long time, TimeUnit unit) throws InterruptedException;
/**
* Causes the current thread to wait until it is signalled or interrupted,
* or the specified deadline elapses.
* 使当前线程等待,直到发出信号或被中断,或者指定的截止日期过去。
*
* <p>The lock associated with this condition is atomically
* released and the current thread becomes disabled for thread scheduling
* purposes and lies dormant until <em>one</em> of five things happens:
* 与此条件相关联的锁被自动释放,当前线程出于线程调度目的被禁用,
* 并处于休眠状态,直到发生以下五种情况之一:
*
* <ul>
* <li>Some other thread invokes the {@link #signal} method for this
* {@code Condition} and the current thread happens to be chosen as the
* thread to be awakened; or
* 其他一些线程为此Condition调用signal方法,而当前线程恰好被选为要唤醒的线程;或
*
*
* <li>Some other thread invokes the {@link #signalAll} method for this
* {@code Condition}; or
* 其他一些线程为此Condition调用signalAll方法;或
*
*
* <li>Some other thread {@linkplain Thread#interrupt interrupts} the
* current thread, and interruption of thread suspension is supported; or
* 其他一些线程对当前线程调用Thread.interrupt(),支持中断线程挂起;或
*
* <li>The specified deadline elapses; or
* 指定的截止日期已过;或
*
* <li>A "<em>spurious wakeup</em>" occurs.
* 出现虚假唤醒
* </ul>
*
* <p>In all cases, before this method can return the current thread must
* re-acquire the lock associated with this condition. When the
* thread returns it is <em>guaranteed</em> to hold this lock.
* 在所有情况下,在该方法返回之前,
* 当前线程必须重新获取与此条件关联的锁。当线程返回时,保证持有此锁。
*
*
* <p>If the current thread:
* 如果当前线程:
* <ul>
*
* <li>has its interrupted status set on entry to this method; or
* 在进入该方法时设置其中断状态;或
*
* <li>is {@linkplain Thread#interrupt interrupted} while waiting
* and interruption of thread suspension is supported,
* 在等待时被Thread.interrupt(),支持中断线程挂起,
* </ul>
* then {@link InterruptedException} is thrown and the current thread's
* interrupted status is cleared. It is not specified, in the first
* case, whether or not the test for interruption occurs before the lock
* is released.
* 然后抛出 InterruptedException,并清除当前线程的中断状态。
* 在第一种情况下,未规定是否在释放锁之前进行中断测试。
*
*
*
* <p>The return value indicates whether the deadline has elapsed,
* which can be used as follows:
* 返回值表示截止日期是否已过,可按如下方式使用:
* <pre> {@code
* boolean aMethod(Date deadline)
* throws InterruptedException {
* boolean stillWaiting = true;
* lock.lock();
* try {
* while (!conditionBeingWaitedFor()) {
* if (!stillWaiting)
* return false;
* stillWaiting = theCondition.awaitUntil(deadline);
* }
* // ...
* return true;
* } finally {
* lock.unlock();
* }
* }}</pre>
*
* <p><b>Implementation Considerations</b>
* 关于实现的一些考虑
*
* <p>The current thread is assumed to hold the lock associated with this
* {@code Condition} when this method is called.
* It is up to the implementation to determine if this is
* the case and if not, how to respond. Typically, an exception will be
* thrown (such as {@link IllegalMonitorStateException}) and the
* implementation must document that fact.
* 调用此方法时,假定当前线程持有与此Condition关联的锁。
* 由实施部门确定是否存在这种情况,如果不是,如何应对。
* 通常,将引发异常(例如IllegalMonitorStateException),实现必须记录该事实。
*
*
*
* <p>An implementation can favor responding to an interrupt over normal
* method return in response to a signal, or over indicating the passing
* of the specified deadline. In either case the implementation
* must ensure that the signal is redirected to another waiting thread, if
* there is one.
* 实现可能有利于响应中断而不是响应信号的正常方法返回,
* 或者有利于响应指定截止日期的通过。
* 在任何一种情况下,实现都必须确保信号被重定向到另一个等待线程(如果有)。
*
*
*
* @param deadline the absolute time to wait until 等待的绝对时间
* @return {@code false} if the deadline has elapsed upon return, else
* {@code true}
* 返回 false,如果返回时截止日期已过,则{@code true}
* @throws InterruptedException if the current thread is interrupted
* (and interruption of thread suspension is supported)
* 如果当前线程被中断(并且支持中断线程挂起)
*
*/
boolean awaitUntil(Date deadline) throws InterruptedException;
/**
* Wakes up one waiting thread.
* 唤醒一个正在等待的线程。
*
* <p>If any threads are waiting on this condition then one
* is selected for waking up. That thread must then re-acquire the
* lock before returning from {@code await}.
* 如果有任何线程在此条件下等待,则会选择一个线程进行唤醒。
* 然后,该线程必须在从wait返回之前重新获取锁。
*
* <p><b>Implementation Considerations</b>
* 关于实现的一些考虑
*
* <p>An implementation may (and typically does) require that the
* current thread hold the lock associated with this {@code
* Condition} when this method is called. Implementations must
* document this precondition and any actions taken if the lock is
* not held. Typically, an exception such as {@link
* IllegalMonitorStateException} will be thrown.
* 调用此方法时,实现可能(并且通常确实)要求当前线程持有与此Condition关联的锁。
* 实现必须记录此先决条件以及未持有锁时所采取的任何操作。
* 通常,会引发IllegalMonitorStateException之类的异常。
*/
void signal();
/**
* Wakes up all waiting threads.
* 唤醒所有等待的线程。
*
* <p>If any threads are waiting on this condition then they are
* all woken up. Each thread must re-acquire the lock before it can
* return from {@code await}.
* 如果有线程在这种情况下等待,那么它们都会被唤醒。
* 每个线程必须重新获取锁,然后才能从await返回。
*
* <p><b>Implementation Considerations</b>
* 关于实现的一些考虑
*
* <p>An implementation may (and typically does) require that the
* current thread hold the lock associated with this {@code
* Condition} when this method is called. Implementations must
* document this precondition and any actions taken if the lock is
* not held. Typically, an exception such as {@link
* IllegalMonitorStateException} will be thrown.
*
* 调用此方法时,实现可能(并且通常确实)要求当前线程持有与此 Condition关联的锁。
* 实现必须记录此先决条件以及未持有锁时所采取的任何操作。
* 通常,会引发IllegalMonitorStateException之类的异常。
*
*
*/
void signalAll();
}