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

本文详细介绍了Java并发包中Condition接口的各种API,包括await、awaitNanos、signal及signalAll等方法的功能与使用场景,特别关注了awaitNanos方法的精确性和在实际编程中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值