Future await
public interface Future<V> extends java.util.concurrent.Future<V> {
/**
* Waits for this future to be completed within the
* specified time limit.
*
* @return {@code true} if and only if the future was completed within
* the specified time limit
*
* @throws InterruptedException
* if the current thread was interrupted
*/
boolean await(long timeoutMillis) throws InterruptedException;
}
future是一个异步消息的返回值,可以调用Future.await(期望等待的时间)来阻塞当线前线,直到future完成,或者期望的时间到期.
这个功能如何实现
- 需要阻塞一定时间,到时间没有结果立即返回
- 有结果就立即返回
如果让我来实现,我能想到的就是写一个死循环,不断的休眠期望时间的几分之一,休眠结束后,判断是否有结果,如果没有.继续休眠.
//期望等待时间
long exceptTime = 10000;
long start = System.currentTimeMillis();
while (true){
//判断是否执行完成,如果执行完成返回
//休眠 exceptTime/10
Thread.sleep(exceptTime/10);
//判断是否超过等待时间,如果超过,返回
if(System.currentTimeMillis()-start>exceptTime){
return false;
}
}
netty DefaultPromise的实现
private boolean await0(long timeoutNanos, boolean interruptable) throws InterruptedException {
//如果完成,直接返回
if (isDone()) {
return true;
}
if (timeoutNanos <= 0) {
return isDone();
}
//如果被中断,抛出异常
if (interruptable && Thread.interrupted()) {
throw new InterruptedException(toString());
}
long startTime = System.nanoTime();
long waitTime = timeoutNanos;
boolean interrupted = false;
try {
synchronized (this) {
if (isDone()) {
return true;
}
if (waitTime <= 0) {
return isDone();
}
checkDeadLock();
incWaiters();
try {
for (;;) {
try {
//调用object的wait方法
wait(waitTime / 1000000, (int) (waitTime % 1000000));
} catch (InterruptedException e) {
if (interruptable) {
throw e;
} else {
interrupted = true;
}
}
if (isDone()) {
return true;
} else {
//等待时间-已经等待的时间,这里应该是interruptable 为false的情况,虽然抛出了中断异常,但还是继续执行下去.
waitTime = timeoutNanos - (System.nanoTime() - startTime);
if (waitTime <= 0) {
return isDone();
}
}
}
} finally {
decWaiters();
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
可以看到主要是调用 Object的wait方法,看来这种需要还是要借助操作系统来实现
object wait方法
public final void wait(long timeout, int nanos) throws InterruptedException
造成当前的线程等待,直到另一个线程为这个object调用notify或notifyAll方法,或者一些其他线程中断(interrupts)当前线程,或者已经过了一定量的时间.
这个方法和一个参数的wait方法是相同的,但是它允许更好的控制时间来在放弃前等待通知,时间用纳秒表示 1000000*timeout+nanos
在其他方面,这个方法和一个参数的wait(long)做一样的事情,尤其是wait(0,0)和wait(0)意味着相同的事情.
当前的线程必须有这个Object的monitor,这个线程释放monitor的所有权等待直到下面的两种情况中的一个发生.
- 其它线程通知在这个object的monitor上等待的线程来唤醒,通过notify或notifyAll方法.
- 由毫秒+纳秒指定的超时时间已经过去了.
这个线程等待直到它可能重新获取monitor的所有权,然后执行.
就像在一个参数版本中,中断和伪唤醒是可能的,这个方法应该总是在循环中使用:
synchronized (obj) {
while (<condition does not hold>)
obj.wait(timeout, nanos);
... // Perform action appropriate to condition
}
这个方法应该被有这个对象monitor的所有权的线程调用.有关线程可以成为监视器所有者的方式的说明,请参阅notify方法.
InterruptedException - 如果任何线程在当前线程等待通知之前或当前线程中断当前线程。 抛出此异常时,将清除当前线程的中断状态。
public final void wait(long timeout, int nanos) throws InterruptedException {
if (timeout < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (nanos < 0 || nanos > 999999) {
throw new IllegalArgumentException(
"nanosecond timeout value out of range");
}
if (nanos >= 500000 || (nanos != 0 && timeout == 0)) {
timeout++;
}
wait(timeout);
}
实现思路
获取object锁后,调用 wait 方法进入等待,如果此时任务完成,调用
if (hasWaiters()) {
notifyAll();
}
主要就是调用object 的wait notifyAll实现.