@Override public synchronized T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { Args.notNull(unit, "Time unit"); final long msecs = unit.toMillis(timeout); final long startTime = (msecs <= 0) ? 0 : System.currentTimeMillis(); long waitTime = msecs; if (this.completed) { return getResult(); } else if (waitTime <= 0) { throw TimeoutValueException.fromMillis(msecs, msecs + Math.abs(waitTime)); } else { for (;;) { wait(waitTime); if (this.completed) { return getResult(); } waitTime = msecs - (System.currentTimeMillis() - startTime); if (waitTime <= 0) { throw TimeoutValueException.fromMillis(msecs, msecs + Math.abs(waitTime)); } } } }
@Override public synchronized T get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { Args.notNull(unit, "Time unit"); final long msecs = unit.toMillis(timeout); final long startTime = (msecs <= 0) ? 0 : System.currentTimeMillis(); long waitTime = msecs; if (this.completed) { return getResult(); } else if (waitTime <= 0) { throw TimeoutValueException.fromMillis(msecs, msecs + Math.abs(waitTime)); } else { for (;;) { wait(waitTime); if (this.completed) { return getResult(); } waitTime = msecs - (System.currentTimeMillis() - startTime); if (waitTime <= 0) { throw TimeoutValueException.fromMillis(msecs, msecs + Math.abs(waitTime)); } } } }
@Test public void testMessage() { Assert.assertEquals("Timeout deadline: 1,000 MILLISECONDS, actual: 2,000 MILLISECONDS", TimeoutValueException.fromMillis(1000, 2000).getMessage()); } }