void await(final Awaitable waitable) throws TimeoutException, InterruptedException { if (!waitable.await(getTime(), getUnit())) { throwTimeoutException(); } }
/** * Get a {@link Timeout} that uses nanosecond precision. The accuracy will * depend on the accuracy of {@link System#nanoTime()}. * * @param time the maximum time to wait for the lock * @param unit the time unit of the <tt>time</tt> argument. * @return timeout with {@link TimeUnit#NANOSECONDS} precision. */ public static Timeout getNanosTimeout(final long time, final TimeUnit unit) { return new Timeout(time, unit, TimeSuppliers.NANOS); }
@Override public boolean apply(final Void input) { return !timeout.isExpired(); } }
public boolean await(final long time, final TimeUnit unit) { final Timeout timeout = Timeout.getNanosTimeout(time, unit); try { return future.get(timeout.getTime(), timeout.getUnit()).await(timeout.getTime(), timeout.getUnit()); } catch (final ExecutionException e) { throw new RuntimeException(e); } catch (final TimeoutException e) { return false; } catch (final InterruptedException e) { throw new RuntimeInterruptedException(e); } }
@Override public T apply(final CompletionService<T> completionService) { try { final Future<T> future = completionService.poll(timeout.getTime(), timeout.getUnit()); if (future == null) { cancelRemaining(); throw timeout.getTimeoutException(); } futures.remove(future); return future.get(); } catch (final InterruptedException e) { throw new RuntimeInterruptedException(e); } catch (final ExecutionException e) { throw new RuntimeExecutionException(e); } }
final Timeout timeout = Timeout.getNanosTimeout(time, unit); V result = null; while (result == null) { timeout.await(latch); result = ref.getAndSet(null);
public RuntimeTimeoutException getTimeoutException() { return new RuntimeTimeoutException(timeoutPeriod, getUnit()); }
@Override public Predicate<Void> get() { return new TimeToLive(getNanosTimeout(time, unit)); } });
/** * Has this timeout expired * * @return true if expired */ public boolean isExpired() { return getTime() <= 0; }
@Override public Predicate<Void> get() { return new TimeToIdle(timeoutFactory(time, unit, NANOS)); } });
public boolean await(final long time, final TimeUnit unit) final Timeout timeout = Timeout.getNanosTimeout(time, unit); for (final Iterator<InFlightResult> it = results.iterator(); it.hasNext();) try if (!result.await(timeout.getTime(), timeout.getUnit()))
/** * Gets the current element if it is not null, if it is null then this method * blocks and waits until it is not null. Unlike {@link #take()} it does not * reset the current element to null. * <p> * If the current thread: * <ul> * <li>has its interrupted status set on entry to this method; or * <li>is {@link Thread#interrupt() interrupted} while waiting, * </ul> * then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * @return the current element * @throws TimeoutException if the timeout is reached without another thread * having called {@link #set(Object)}. * @throws InterruptedException if the current thread is interrupted while * waiting */ @NotNull public V get(final long time, @NotNull final TimeUnit unit) throws TimeoutException, InterruptedException { final Timeout timeout = Timeout.getNanosTimeout(time, unit); V result = ref.get(); while (result == null) { timeout.await(latch); result = ref.get(); } return result; }
/** * Always throws a {@link TimeoutException}. * * @throws TimedOutException, always. */ public void throwTimeoutException() throws TimedOutException { throw new TimedOutException(timeoutPeriod, getUnit()); }
/** * Version of {@link #invokeAll(Iterable)} that supports a timeout. Any jobs * that are not complete by the timeout are interrupted and discarded. * * @param <T> the result type * @param callables the jobs to run * @param time the max time spent per job specified by: * @param unit the TimeUnit time is specified in * @return an Iterable that returns the results in the order in which they * return, excluding any null values. * * @see #invokeAll(Iterable) * @since 2.1 */ public <T> Iterable<T> invokeAll(final Iterable<? extends Callable<T>> callables, final long time, final TimeUnit unit) { return invokeAllTasks(callables, new TimeoutAccessor<T>(getNanosTimeout(time, unit))); }
/** * Get a {@link Timeout} that uses millisecond precision. The accuracy will * depend on the accuracy of {@link System#currentTimeMillis()}. * * @param time the maximum time to wait for the lock * @param unit the time unit of the <tt>time</tt> argument. * @return timeout with {@link TimeUnit#MILLISECONDS} precision. */ public static Timeout getMillisTimeout(final long time, final TimeUnit unit) { return new Timeout(time, unit, TimeSuppliers.MILLIS); }
@Override public boolean apply(final Void input) { final boolean alive = !lastAccess.isExpired(); if (alive) { lastAccess = timeout.get(); } return alive; } }
@Override public Timeout get() { return new Timeout(time, unit, supplier); } };