java多线程基本概述(十)——Condition

Condition共有一下几个api:

void       await() //Causes the current thread to wait until it is signalled or interrupted. //不支持超时等待
boolean await(long time, TimeUnit unit) //Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.//支持所有
long awaitNanos(long nanosTimeout) //Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.//和上面等价
void awaitUninterruptibly() //Causes the current thread to wait until it is signalled. 不支持中断,只有这个不支持中断响应,所以不会抛中断异常。
boolean awaitUntil(Date deadline) //Causes the current thread to wait until it is signalled or interrupted, or the specified deadline elapses.//等待awiatNanos()。
void signal() //Wakes up one waiting thread.
void signalAll() //Wakes up all waiting threads.

awaitNanos()api:

long awaitNanos(long nanosTimeout)
                throws InterruptedException
Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.
The lock associated with this condition is atomically released and the current thread becomes disabled for thread scheduling purposes and lies dormant until one of five things happens:

Some other thread invokes the signal() method for this Condition and the current thread happens to be chosen as the thread to be awakened; or
Some other thread invokes the signalAll() method for this Condition; or
Some other thread interrupts the current thread, and interruption of thread suspension is supported; or
The specified waiting time elapses; or
A "spurious wakeup" occurs.
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 guaranteed to hold this lock.

If the current thread:

has its interrupted status set on entry to this method; or
is interrupted while waiting and interruption of thread suspension is supported,
then 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.
The method returns an estimate of the number of nanoseconds remaining to wait given the supplied 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:

 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();
   }
 }
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.

翻译如下:

造成当前线程在接到信号、被中断或到达指定等待时间之前一直处于等待状态。
与此条件相关的锁以原子方式释放,并且出于线程调度的目的,将禁用当前线程,且在发生以下五种情况之一 以前,当前线程将一直处于休眠状态:

其他某个线程调用此 Condition 的 signal() 方法,并且碰巧将当前线程选为被唤醒的线程;或者
其他某个线程调用此 Condition 的 signalAll() 方法;或者
其他某个线程中断当前线程,且支持中断线程的挂起;或者
已超过指定的等待时间;或者
发生“虚假唤醒”。
在所有情况下,在此方法可以返回当前线程之前,都必须重新获取与此条件有关的锁。在线程返回时,可以保证 它保持此锁。

如果当前线程:

在进入此方法时已经设置了该线程的中断状态;或者
在支持等待和中断线程挂起时,线程被中断,
则抛出 InterruptedException,并且清除当前线程的已中断状态。在第一种情况下,没有指定是否在释放锁之前发生中断测试。
在返回时,该方法返回了所剩毫微秒数的一个估计值,以等待所提供的 nanosTimeout 值的时间,如果超时,则返回一个小于等于 0 的值。可以用此值来确定在等待返回但某一等待条件仍不具备的情况下,是否要再次等待,以及再次等待的时间。此方法的典型用法采用以下形式:
原文地址:https://www.cnblogs.com/soar-hu/p/6732488.html