/** as {@link #sleep(long)} */ public static void sleep(Duration duration) { Time.sleep(duration.toMillisecondsRoundingUp()); }
@Override public synchronized boolean blockUntilStarted(Duration timeout) { Long endTime = timeout==null ? null : System.currentTimeMillis() + timeout.toMillisecondsRoundingUp(); while (true) { if (cancelled) throw new CancellationException(); if (result==null) try { if (timeout==null) { wait(); } else { long remaining = endTime - System.currentTimeMillis(); if (remaining>0) wait(remaining); else return false; } } catch (InterruptedException e) { Thread.currentThread().interrupt(); Throwables.propagate(e); } if (result!=null) return true; } }
@Override public T get(Duration duration) throws InterruptedException, ExecutionException, TimeoutException { long start = System.currentTimeMillis(); Long end = duration==null ? null : start + duration.toMillisecondsRoundingUp(); while (end==null || end > System.currentTimeMillis()) { if (cancelled) throw new CancellationException(); if (result == null) { synchronized (this) { long remaining = end - System.currentTimeMillis(); if (result==null && remaining>0) wait(remaining); } } if (result != null) break; } Long remaining = end==null ? null : end - System.currentTimeMillis(); if (isDone()) { return result.get(1, TimeUnit.MILLISECONDS); } else if (remaining == null) { return result.get(); } else if (remaining > 0) { return result.get(remaining, TimeUnit.MILLISECONDS); } else { throw new TimeoutException(); } }
@Override public boolean blockUntilEnded(Duration timeout) { Long endTime = timeout==null ? null : System.currentTimeMillis() + timeout.toMillisecondsRoundingUp(); try { boolean started = blockUntilStarted(timeout); if (!started) return false; if (timeout==null) { result.get(); } else { long remaining = endTime - System.currentTimeMillis(); if (remaining>0) result.get(remaining, TimeUnit.MILLISECONDS); } return isDone(); } catch (Throwable t) { if (log.isDebugEnabled()) log.debug("call from "+Thread.currentThread()+" blocking until "+this+" finishes ended with error: "+t); /* contract is just to log errors at debug, otherwise do nothing */ return isDone(); } }