Java Condition 源码
前言
相关系列
- 《Java & Condition & 目录》
- 《Java & Condition & 源码》
- 《Java & Condition & 总结》
- 《Java & Condition & 问题》
涉及内容
- 《Java & AQS & 总结》
- 《Java & Lock & 总结》
- 《Java & Lock/AQS & ReentrantLock & 总结》
源码
/** ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*********************//******* Written by Doug Lea with assistance from members of JCP JSR-166* Expert Group and released to the public domain, as explained at* http://creativecommons.org/publicdomain/zero/1.0/*/package juc.locks;import juc.ArrayBlockingQueue;
import juc.TimeUnit;import java.util.Date;/*** {@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.* 条件析构出对象监视方法wait、notify及notifyAll至一个不同的对象中以产生令每个对象拥有多重等待集的效果,并通过任* 意锁实现的使用来组合它们。当锁代替synchronized方法和语句的使用是,条件代替对象监视方法的使用。* <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.* 条件实例被固定绑定于一个锁。为了获取一个指定锁实例的条件实例可以使用其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.* 作为一个实例,假设我们有一块受限制的缓存以支持防止和获取方法。如果一个获取在一块空缓存中尝试,那线程将阻塞* 直至一个项变得可用;如果一个放置在一块满的缓存中尝试,那么线程将阻塞直至一块空间变得可用。我们希望放置线程* 和拿取线程在不同的等待集中保持等待,这样我们可以使用当项或空间在缓存中变得可用时在一个时间只通知单集线程的* 最优化。这可以使用两个条件实例达到。* <pre>* class BoundedBuffer {* <b>final Lock lock = new ReentrantLock();</b>* final Condition notFull = <b>lock.newCondition(); </b>* final Condition notEmpty = <b>lock.newCondition(); </b>** final Object[] items = new Object[100];* int putptr, takeptr, count;** public void put(Object x) throws InterruptedException {* <b>lock.lock();* try {</b>* while (count == items.length)* <b>notFull.await();</b>* items[putptr] = x;* if (++putptr == items.length) putptr = 0;* ++count;* <b>notEmpty.signal();</b>* <b>} finally {* lock.unlock();* }</b>* }** public Object take() throws InterruptedException {* <b>lock.lock();* try {</b>* while (count == 0)* <b>notEmpty.await();</b>* Object x = items[takeptr];* if (++takeptr == items.length) takeptr = 0;* --count;* <b>notFull.signal();</b>* return x;* <b>} finally {* lock.unlock();* }</b>* }* }* </pre>* <p>* (The {@link ArrayBlockingQueue} class provides this functionality, so there is no reason to implement this sample* usage class.)* 一个数组阻塞队列提供这个功能,所以没有理由实现这个样品用法的类)* <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.* 一个条件实现可以提供行为及与对象监视方法不同的语义,例如保证通知的顺序,或者当前执行通知是或不获取用于持有* 的锁。如果一个实现提供如此指定的语义那么实现必须记录这些语义。* <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 notification} methods invoked. 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.* 注意:条件实例只是正常的对象并且可以在synchronized语句中作为目标使用,并且可以调用它们所拥有的监视器wait()和* notify()方法。获取一个条件实例的监视器锁,或者使用它的监视器方法与获取条件所关联的锁或使用它的await()及signal()* 方法没有任何特殊关联。推荐你永远不要时候用这种方式使用条件实例以避免混淆,除非在它们拥有的实现中可能发生。* <p>* Except where noted, passing a {@code null} value for any parameter will result in a {@link NullPointerException}* being thrown.* 除了这些注意,为任何参数传递一个null将导致抛出空异常。* <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.* 当一个条件等待后,一个虚假的唤醒已允许发生(即在不需要未达到条件的情况下唤醒),其一般会在平台语义下作为妥* 协。这在大部分应用程序中会存在一点实际冲击,例如一个条件应该经常在一个循环后等待以测试等待后的状态谓语。一* 个实现可自由移除虚假唤醒(即不进行虚假唤醒)但推荐程序开发者经常假设它们会发生并且经常在一个循环中等待。* <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.* 中断通常代表异常,并且检查中断通常是不频繁的,一个实现可以倾向于回应中断(即在实现中判断线程是否被中断)而不是正* 常的方法返回。这是正确的,即使它可能显示中断发生在可能已被解除线程阻塞的其它活动之后。一个实现应该记录这些行为。** @author Doug Lea* @since 1.5*/
public interface Condition {/*** Causes the current thread to wait until it is signalled or {@linkplain Thread#interrupt interrupted}.* 令当前线程等待直至它被通知或被中断。* <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:* 与当前关联的锁会原子性地释放并且当前线程处于线程调度的目的会变得无效并休眠至四种情况的其中之一发生:* <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 <li>Some other thread invokes the {@link #signalAll}* method for this {@code Condition}; or <li>Some other thread {@linkplain Thread#interrupt interrupts} the current* thread, and interruption of thread suspension is supported; or <li>A "<em>spurious wakeup</em>"* occurs.* </ul>* 某些其它线程调用当前条件的signal方法并且当前线程正好被选为被唤醒的线程;或者某些其它线程调用当前条件的* signalAll方法;或者某些线程中断当前线程,并且线程中断支持暂停;或者虚假的唤醒发生。* <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,* 在进入当前方法时已被中断;或者在等待期间被中断并且线程中断支持暂停。* </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.* 那么将抛出中断异常并且当前线程的中断状态将被清除。在第一种情况下,没有指定中断测试是否在锁被释放之前发生。* <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.* 当当前方法被调用时,当前方法被假设持有当前条件所关联的锁。这取决于实现在这种情况下如何决定,已经不在这种* 情况下如果回应。通常,这将抛出一个非法监视状态异常,吧行窃实现必须记录这种真相。* <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)* 中断异常:如果当前线程被中断(并且线程中断支持暂停)* @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 等待* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 令持有当前条件关联锁的当前线程原子地释放锁并进入等待状态,直至因为信号而唤醒并重新持有当前条件的关联锁后* 返回。如果调用该方法时当前线程未持有当前条件的关联锁则通常会直接抛出异常,但实现类也可自定义逻辑。此外,* 如果当前线程在调方法时已被中断或在等待期间被中断则将抛出中断异常,且当前线程的中断状态会被清除。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/void await() throws InterruptedException;/*** Causes the current thread to wait until it is signalled.* 令当前线程等待直至被通知。* <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 <li>Some other thread invokes the {@link #signalAll}* method for this {@code Condition}; or <li>A "<em>spurious wakeup</em>" occurs.* </ul>* 某些其它线程调用当前条件的signal方法并且当前线程正好被选为唤醒线程;或者某些其它线程调用当前条件的signalAll* 方法;或者一次虚假的唤醒发生。* <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.* 如果当当前线程进入当前方法时它的中断状态已被设置;或者在等待期间被中断,它将继续等待至被通知。当它最终从* 当前方法返回时它的中断状态将依然被设置。* <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.* 当当前方法被调用时当前线程被假设持有当前条件锁关联的锁。如果是这种情况这取决于实现确定,以及如果不是这种* 情况该如何回应。通常,这将抛出一个异常(例如非法监视状态异常)以及实现必须记录事实。** @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 等待(不中断)* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 令持有当前条件关联锁的当前线程原子地释放锁并进入等待状态,直至因为信号而唤醒并重新持有当前条件的关联锁后* 返回。如果调用该方法时当前线程未持有当前条件的关联锁则通常会直接抛出异常,但实现类也可自定义逻辑。此外,* 如果当前线程在调方法时已被中断或在等待期间被中断不会抛出中断异常,但当前线程的中断状态会被保留。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/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 <li>Some other thread invokes the {@link #signalAll}* method for this {@code Condition}; or <li>Some other thread {@linkplain Thread#interrupt interrupts} the current* thread, and interruption of thread suspension is supported; or <li>The specified waiting time elapses; or <li>A* "<em>spurious wakeup</em>" occurs.* </ul>* 某些其它线程调用当前条件的signal方法并且当前线程正好被选为被唤醒的线程;或者某些其它线程调用当前条件的* signalAll方法;或者某些线程中断当前线程,并且线程中断支持暂停;或者指定等待时间消逝;或者虚假的唤醒发生。* <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,* 在进入当前方法时已被中断;或者等待过程中被中断且线程支持暂停,* </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.* 那么将抛出中断异常并且当前线程的中断状态将被清除。这并不特殊,在第一种情况中,应在锁被释放之前测试中断发* 生(即在线程结束等待的代码位置需要判断线程是否发生中断)。* <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:* 方法会返回指定提供的纳秒时间值尚未等待的纳秒估计值,或者如果超时则返回一个小于或等于0的值。该值可以用于* 确定是否以及在这种等待已经返回单条件依然没有持有的情况中重等待多久。当前方法的典型使用如下格式:* <pre> {@code* boolean aMethod(long timeout, TimeUnit unit) {* long nanos = unit.toNanos(timeout);* lock.lock();* try {* while (!conditionBeingWaitedFor()) {* if (nanos <= 0L)* return false;* // 将返回的时间作为新的指定等待时间。* nanos = theCondition.awaitNanos(nanos);* }* // ...* } 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.* 当当前方法被调用时当前线程被假设持有当前条件锁关联的锁。如果是这种情况这取决于实现确定,以及如果不是这种* 情况该如何回应。通常,这将抛出一个异常(例如非法监视状态异常)以及实现必须记录事实。* <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.* 从当前方法返回后纳秒时间值减去花费等待时间的估计值。一个正值可能被用于作为参数用于随后调用当前方法以结束* 等待到期望时间。一个小于或等于0的值意味着无时间剩余。* @throws InterruptedException if the current thread is interrupted (and interruption of thread suspension is* supported)* 中断异常:如果当前线程被中断(并且被暂停的线程支持中断)* @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 等待纳秒* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 令持有当前条件关联锁的当前线程原子地释放锁并进入等待状态,直至因为信号而唤醒并重新持有当前条件的关联锁后* 返回。如果调用该方法时当前线程未持有当前条件的关联锁则通常会直接抛出异常,但实现类也可自定义逻辑。此外,* 如果当前线程在调方法时已被中断或在等待期间被中断则将抛出中断异常,且当前线程的中断状态会被清除。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/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>* 令当前线程等待直至被通知或被中断,或者指定等待时间消逝。该方法在行为上等价于:awaitNanos(unit.toNanos(time)** @param time the maximum time to wait 用于等待的最大时间* @param unit the time unit of the {@code time} argument 时间参数的时间单位* @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)* 中断异常:如果当前线程被中断(并且被暂停的线程支持中断)* @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 等待* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 令持有当前条件关联锁的当前线程原子地释放锁并进入等待状态,直至因为信号或指定等待时间超时而唤醒并重新持有* 当前条件的关联锁后返回。如果当前线程因为信号而唤醒则返回true;否则返回false。如果调用该方法时当前线程未持* 有当前条件的关联锁则通常会直接抛出异常,但实现类也可自定义逻辑。此外,如果当前线程在调方法时已被中断或在* 等待期间被中断则将抛出中断异常,且当前线程的中断状态会被清除。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/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 <li>Some other thread invokes the {@link #signalAll} method for this* {@code Condition}; or <li>Some other thread {@linkplain Thread#interrupt interrupts} the current thread, and* interruption of thread suspension is supported; or <li>The specified deadline elapses; or <li>A "<em>spurious* wakeup</em>" occurs.* </ul>* 某些其它线程调用当前条件的signal方法并且当前线程正好被选为被唤醒的线程;或者某些其它线程调用当前条件的* signalAll方法;或者某些线程中断当前线程,并且暂停线程支持中断;或者指定最后期限消逝;或者虚假的唤醒发生。* <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,* 在进入当前方法时已被中断;或者等待过程中被中断且线程支持暂停,* </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.* 那么将抛出中断异常并且当前线程的中断状态将被清除。这并不特殊,在第一种情况中,应在锁被释放之前测试中断发* 生(即在线程结束等待的代码位置需要判断线程是否发生中断)。* <p>* The return value indicates whether the deadline has elapsed, which can be used as follows:* 返回值代表着最后期限是否消逝,可以按以下方式使用:* <pre> {@code* boolean aMethod(Date deadline) {* boolean stillWaiting = true;* lock.lock();* try {* while (!conditionBeingWaitedFor()) {* if (!stillWaiting)* return false;* stillWaiting = theCondition.awaitUntil(deadline);* // stillWaiting为false意味着超时。* }* // ...* } 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.* 当当前方法被调用时当前线程被假设持有当前条件锁关联的锁。如果是这种情况这取决于实现确定,以及如果不是这种* 情况该如何回应。通常,这将抛出一个异常(例如非法监视状态异常)以及实现必须记录事实。* <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;否则返回true。* @throws InterruptedException if the current thread is interrupted (and interruption of thread suspension is* supported)* 中断异常:如果当前线程被中断(并且被暂停的线程支持中断)* @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 等待直至* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 令持有当前条件关联锁的当前线程原子地释放锁并进入等待状态,直至因为信号或指定等待时间超时而唤醒并重新持有* 当前条件的关联锁后返回。如果当前线程因为信号而唤醒则返回true;否则返回false。如果调用该方法时当前线程未持* 有当前条件的关联锁则通常会直接抛出异常,但实现类也可自定义逻辑。此外,如果当前线程在调方法时已被中断或在* 等待期间被中断则将抛出中断异常,且当前线程的中断状态会被清除。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/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}.* 如果任意线程在当前条件中等待那么唤醒被选中的。该线程必须在从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.* 一个实现可能(典型地)获取在当前方法被调用时令当前线程持有当前条件所关联的锁。实现必须记录该先决条件以* 及如果锁不被持有的情况下采取的活动。通常,这将抛出一个例如非法监视状态异常的异常。** @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 信号* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 唤醒一条在当前条件中等待的线程以令之重新持有当前条件的关联锁。如果当前条件中没有正在等待的线程则该方法将* 没有任何作用,并且如果当前线程在调用该方法时为持有当前条件的关联锁则通常会抛出例如例如非法监视状态异常的* 异常,但实现也可以自定义逻辑。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/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.* 一个实现可能(典型地)获取在当前方法被调用时令当前线程持有当前条件所关联的锁。实现必须记录该先决条件以* 及如果锁不被持有的情况下采取的活动。通常,这将抛出一个例如非法监视状态异常的异常。** @Description: ----------------------------------------------------------- 名称 -----------------------------------------------------------* 信号全部* @Description: ----------------------------------------------------------- 作用 -----------------------------------------------------------* 唤醒所有在当前条件中等待的线程以令之重新持有当前条件的关联锁。如果当前条件中没有正在等待的线程则该方法将* 没有任何作用,并且如果当前线程在调用该方法时为持有当前条件的关联锁则通常会抛出例如例如非法监视状态异常的* 异常,但实现也可以自定义逻辑。* @Description: ----------------------------------------------------------- 逻辑 -----------------------------------------------------------* ~*/void signalAll();
}