public Object poll(long timeout, TimeUnit unit)
throws InterruptedException {
long nanos = unit.toNanos(timeout);
long deadline = Utils.nanoTime() + nanos;
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
for (;;) {
RunnableScheduledFuture first = queue[0];
if (first == null) {
if (nanos <= 0) {
return null;
} else {
available.await(nanos, TimeUnit.NANOSECONDS);
nanos = deadline - Utils.nanoTime();
}
} else {
long delay = first.getDelay(TimeUnit.NANOSECONDS);
if (delay > 0) {
if (nanos <= 0)
return null;
if (delay > nanos)
delay = nanos;
available.await(delay, TimeUnit.NANOSECONDS);
nanos = deadline - Utils.nanoTime();
} else
return finishPoll(first);
}
}
} finally {
lock.unlock();
}
}