<V> ScheduledFuture<V> schedulePendingCallable(PendingCallable<V> callable) { if (shutdown.get()) { throw new IllegalStateException("This executor has been shutdown"); } synchronized (pendingCallables) { pendingCallables.add(callable); } work(); synchronized (expectedWorkQueue) { // We compare by the callable delay in order decide when to remove expectations from the // expected work queue, i.e. only the expected work that matches the delay of the scheduled // callable is removed from the queue. if (!expectedWorkQueue.isEmpty() && expectedWorkQueue.peek().equals(callable.delay)) { expectedWorkQueue.poll(); } expectedWorkQueue.notifyAll(); } return callable.getScheduledFuture(); }
@Override public boolean equals(Object obj) { if (obj instanceof OffsetClock) { OffsetClock other = (OffsetClock) obj; return baseClock.equals(other.baseClock) && offset.equals(other.offset); } return false; } @Override
@Override public boolean equals(Object obj) { if (obj instanceof OffsetClock) { OffsetClock other = (OffsetClock) obj; return baseClock.equals(other.baseClock) && offset.equals(other.offset); } return false; } @Override
/** * Obtains a clock that returns instants from the specified clock with the * specified duration added * <p> * This clock wraps another clock, returning instants that are later by the * specified duration. If the duration is negative, the instants will be * earlier than the current date and time. * The main use case for this is to simulate running in the future or in the past. * <p> * A duration of zero would have no offsetting effect. * Passing zero will return the underlying clock. * <p> * The returned implementation is immutable, thread-safe and {@code Serializable} * providing that the base clock is. * * @param baseClock the base clock to add the duration to, not null * @param offsetDuration the duration to add, not null * @return a clock based on the base clock with the duration added, not null */ public static Clock offset(Clock baseClock, Duration offsetDuration) { Jdk8Methods.requireNonNull(baseClock, "baseClock"); Jdk8Methods.requireNonNull(offsetDuration, "offsetDuration"); if (offsetDuration.equals(Duration.ZERO)) { return baseClock; } return new OffsetClock(baseClock, offsetDuration); }
/** * Obtains a clock that returns instants from the specified clock with the * specified duration added * <p> * This clock wraps another clock, returning instants that are later by the * specified duration. If the duration is negative, the instants will be * earlier than the current date and time. * The main use case for this is to simulate running in the future or in the past. * <p> * A duration of zero would have no offsetting effect. * Passing zero will return the underlying clock. * <p> * The returned implementation is immutable, thread-safe and {@code Serializable} * providing that the base clock is. * * @param baseClock the base clock to add the duration to, not null * @param offsetDuration the duration to add, not null * @return a clock based on the base clock with the duration added, not null */ public static Clock offset(Clock baseClock, Duration offsetDuration) { Jdk8Methods.requireNonNull(baseClock, "baseClock"); Jdk8Methods.requireNonNull(offsetDuration, "offsetDuration"); if (offsetDuration.equals(Duration.ZERO)) { return baseClock; } return new OffsetClock(baseClock, offsetDuration); }