001/*
002 * Copyright (C) 2011 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 */
014
015package com.google.common.util.concurrent;
016
017import static com.google.common.base.Preconditions.checkArgument;
018import static com.google.common.base.Preconditions.checkNotNull;
019import static com.google.common.util.concurrent.Futures.immediateCancelledFuture;
020import static com.google.common.util.concurrent.Internal.toNanosSaturated;
021import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
022import static com.google.common.util.concurrent.Platform.restoreInterruptIfIsInterruptedException;
023import static java.util.Objects.requireNonNull;
024import static java.util.concurrent.TimeUnit.NANOSECONDS;
025
026import com.google.common.annotations.GwtIncompatible;
027import com.google.common.annotations.J2ktIncompatible;
028import com.google.errorprone.annotations.CanIgnoreReturnValue;
029import com.google.errorprone.annotations.concurrent.GuardedBy;
030import com.google.j2objc.annotations.WeakOuter;
031import java.time.Duration;
032import java.util.concurrent.Callable;
033import java.util.concurrent.Executor;
034import java.util.concurrent.Executors;
035import java.util.concurrent.Future;
036import java.util.concurrent.ScheduledExecutorService;
037import java.util.concurrent.ScheduledFuture;
038import java.util.concurrent.ThreadFactory;
039import java.util.concurrent.TimeUnit;
040import java.util.concurrent.TimeoutException;
041import java.util.concurrent.locks.ReentrantLock;
042import java.util.logging.Level;
043import javax.annotation.CheckForNull;
044import org.checkerframework.checker.nullness.qual.Nullable;
045
046/**
047 * Base class for services that can implement {@link #startUp} and {@link #shutDown} but while in
048 * the "running" state need to perform a periodic task. Subclasses can implement {@link #startUp},
049 * {@link #shutDown} and also a {@link #runOneIteration} method that will be executed periodically.
050 *
051 * <p>This class uses the {@link ScheduledExecutorService} returned from {@link #executor} to run
052 * the {@link #startUp} and {@link #shutDown} methods and also uses that service to schedule the
053 * {@link #runOneIteration} that will be executed periodically as specified by its {@link
054 * Scheduler}. When this service is asked to stop via {@link #stopAsync} it will cancel the periodic
055 * task (but not interrupt it) and wait for it to stop before running the {@link #shutDown} method.
056 *
057 * <p>Subclasses are guaranteed that the life cycle methods ({@link #runOneIteration}, {@link
058 * #startUp} and {@link #shutDown}) will never run concurrently. Notably, if any execution of {@link
059 * #runOneIteration} takes longer than its schedule defines, then subsequent executions may start
060 * late. Also, all life cycle methods are executed with a lock held, so subclasses can safely modify
061 * shared state without additional synchronization necessary for visibility to later executions of
062 * the life cycle methods.
063 *
064 * <h3>Usage Example</h3>
065 *
066 * <p>Here is a sketch of a service which crawls a website and uses the scheduling capabilities to
067 * rate limit itself.
068 *
069 * <pre>{@code
070 * class CrawlingService extends AbstractScheduledService {
071 *   private Set<Uri> visited;
072 *   private Queue<Uri> toCrawl;
073 *   protected void startUp() throws Exception {
074 *     toCrawl = readStartingUris();
075 *   }
076 *
077 *   protected void runOneIteration() throws Exception {
078 *     Uri uri = toCrawl.remove();
079 *     Collection<Uri> newUris = crawl(uri);
080 *     visited.add(uri);
081 *     for (Uri newUri : newUris) {
082 *       if (!visited.contains(newUri)) { toCrawl.add(newUri); }
083 *     }
084 *   }
085 *
086 *   protected void shutDown() throws Exception {
087 *     saveUris(toCrawl);
088 *   }
089 *
090 *   protected Scheduler scheduler() {
091 *     return Scheduler.newFixedRateSchedule(0, 1, TimeUnit.SECONDS);
092 *   }
093 * }
094 * }</pre>
095 *
096 * <p>This class uses the life cycle methods to read in a list of starting URIs and save the set of
097 * outstanding URIs when shutting down. Also, it takes advantage of the scheduling functionality to
098 * rate limit the number of queries we perform.
099 *
100 * @author Luke Sandberg
101 * @since 11.0
102 */
103@GwtIncompatible
104@J2ktIncompatible
105public abstract class AbstractScheduledService implements Service {
106  private static final LazyLogger logger = new LazyLogger(AbstractScheduledService.class);
107
108  /**
109   * A scheduler defines the policy for how the {@link AbstractScheduledService} should run its
110   * task.
111   *
112   * <p>Consider using the {@link #newFixedDelaySchedule} and {@link #newFixedRateSchedule} factory
113   * methods, these provide {@link Scheduler} instances for the common use case of running the
114   * service with a fixed schedule. If more flexibility is needed then consider subclassing {@link
115   * CustomScheduler}.
116   *
117   * @author Luke Sandberg
118   * @since 11.0
119   */
120  public abstract static class Scheduler {
121    /**
122     * Returns a {@link Scheduler} that schedules the task using the {@link
123     * ScheduledExecutorService#scheduleWithFixedDelay} method.
124     *
125     * @param initialDelay the time to delay first execution
126     * @param delay the delay between the termination of one execution and the commencement of the
127     *     next
128     * @since 28.0 (but only since 33.4.0 in the Android flavor)
129     */
130    public static Scheduler newFixedDelaySchedule(Duration initialDelay, Duration delay) {
131      return newFixedDelaySchedule(
132          toNanosSaturated(initialDelay), toNanosSaturated(delay), NANOSECONDS);
133    }
134
135    /**
136     * Returns a {@link Scheduler} that schedules the task using the {@link
137     * ScheduledExecutorService#scheduleWithFixedDelay} method.
138     *
139     * @param initialDelay the time to delay first execution
140     * @param delay the delay between the termination of one execution and the commencement of the
141     *     next
142     * @param unit the time unit of the initialDelay and delay parameters
143     */
144    @SuppressWarnings("GoodTime") // should accept a java.time.Duration
145    public static Scheduler newFixedDelaySchedule(
146        final long initialDelay, final long delay, final TimeUnit unit) {
147      checkNotNull(unit);
148      checkArgument(delay > 0, "delay must be > 0, found %s", delay);
149      return new Scheduler() {
150        @Override
151        public Cancellable schedule(
152            AbstractService service, ScheduledExecutorService executor, Runnable task) {
153          return new FutureAsCancellable(
154              executor.scheduleWithFixedDelay(task, initialDelay, delay, unit));
155        }
156      };
157    }
158
159    /**
160     * Returns a {@link Scheduler} that schedules the task using the {@link
161     * ScheduledExecutorService#scheduleAtFixedRate} method.
162     *
163     * @param initialDelay the time to delay first execution
164     * @param period the period between successive executions of the task
165     * @since 28.0 (but only since 33.4.0 in the Android flavor)
166     */
167    public static Scheduler newFixedRateSchedule(Duration initialDelay, Duration period) {
168      return newFixedRateSchedule(
169          toNanosSaturated(initialDelay), toNanosSaturated(period), NANOSECONDS);
170    }
171
172    /**
173     * Returns a {@link Scheduler} that schedules the task using the {@link
174     * ScheduledExecutorService#scheduleAtFixedRate} method.
175     *
176     * @param initialDelay the time to delay first execution
177     * @param period the period between successive executions of the task
178     * @param unit the time unit of the initialDelay and period parameters
179     */
180    @SuppressWarnings("GoodTime") // should accept a java.time.Duration
181    public static Scheduler newFixedRateSchedule(
182        final long initialDelay, final long period, final TimeUnit unit) {
183      checkNotNull(unit);
184      checkArgument(period > 0, "period must be > 0, found %s", period);
185      return new Scheduler() {
186        @Override
187        public Cancellable schedule(
188            AbstractService service, ScheduledExecutorService executor, Runnable task) {
189          return new FutureAsCancellable(
190              executor.scheduleAtFixedRate(task, initialDelay, period, unit));
191        }
192      };
193    }
194
195    /** Schedules the task to run on the provided executor on behalf of the service. */
196    abstract Cancellable schedule(
197        AbstractService service, ScheduledExecutorService executor, Runnable runnable);
198
199    private Scheduler() {}
200  }
201
202  /* use AbstractService for state management */
203  private final AbstractService delegate = new ServiceDelegate();
204
205  @WeakOuter
206  private final class ServiceDelegate extends AbstractService {
207
208    // A handle to the running task so that we can stop it when a shutdown has been requested.
209    // These two fields are volatile because their values will be accessed from multiple threads.
210    @CheckForNull private volatile Cancellable runningTask;
211    @CheckForNull private volatile ScheduledExecutorService executorService;
212
213    // This lock protects the task so we can ensure that none of the template methods (startUp,
214    // shutDown or runOneIteration) run concurrently with one another.
215    // TODO(lukes): why don't we use ListenableFuture to sequence things? Then we could drop the
216    // lock.
217    private final ReentrantLock lock = new ReentrantLock();
218
219    @WeakOuter
220    class Task implements Runnable {
221      @Override
222      public void run() {
223        lock.lock();
224        try {
225          /*
226           * requireNonNull is safe because Task isn't run (or at least it doesn't succeed in taking
227           * the lock) until after it's scheduled and the runningTask field is set.
228           */
229          if (requireNonNull(runningTask).isCancelled()) {
230            // task may have been cancelled while blocked on the lock.
231            return;
232          }
233          AbstractScheduledService.this.runOneIteration();
234        } catch (Throwable t) {
235          restoreInterruptIfIsInterruptedException(t);
236          try {
237            shutDown();
238          } catch (Exception ignored) {
239            restoreInterruptIfIsInterruptedException(ignored);
240            logger
241                .get()
242                .log(
243                    Level.WARNING,
244                    "Error while attempting to shut down the service after failure.",
245                    ignored);
246          }
247          notifyFailed(t);
248          // requireNonNull is safe now, just as it was above.
249          requireNonNull(runningTask).cancel(false); // prevent future invocations.
250        } finally {
251          lock.unlock();
252        }
253      }
254    }
255
256    private final Runnable task = new Task();
257
258    @Override
259    protected final void doStart() {
260      executorService =
261          MoreExecutors.renamingDecorator(executor(), () -> serviceName() + " " + state());
262      executorService.execute(
263          () -> {
264            lock.lock();
265            try {
266              startUp();
267              /*
268               * requireNonNull is safe because executorService is never cleared after the
269               * assignment above.
270               */
271              requireNonNull(executorService);
272              runningTask = scheduler().schedule(delegate, executorService, task);
273              notifyStarted();
274            } catch (Throwable t) {
275              restoreInterruptIfIsInterruptedException(t);
276              notifyFailed(t);
277              if (runningTask != null) {
278                // prevent the task from running if possible
279                runningTask.cancel(false);
280              }
281            } finally {
282              lock.unlock();
283            }
284          });
285    }
286
287    @Override
288    protected final void doStop() {
289      // Both requireNonNull calls are safe because doStop can run only after a successful doStart.
290      requireNonNull(runningTask);
291      requireNonNull(executorService);
292      runningTask.cancel(false);
293      executorService.execute(
294          () -> {
295            try {
296              lock.lock();
297              try {
298                if (state() != State.STOPPING) {
299                  // This means that the state has changed since we were scheduled. This implies
300                  // that an execution of runOneIteration has thrown an exception and we have
301                  // transitioned to a failed state, also this means that shutDown has already
302                  // been called, so we do not want to call it again.
303                  return;
304                }
305                shutDown();
306              } finally {
307                lock.unlock();
308              }
309              notifyStopped();
310            } catch (Throwable t) {
311              restoreInterruptIfIsInterruptedException(t);
312              notifyFailed(t);
313            }
314          });
315    }
316
317    @Override
318    public String toString() {
319      return AbstractScheduledService.this.toString();
320    }
321  }
322
323  /** Constructor for use by subclasses. */
324  protected AbstractScheduledService() {}
325
326  /**
327   * Run one iteration of the scheduled task. If any invocation of this method throws an exception,
328   * the service will transition to the {@link Service.State#FAILED} state and this method will no
329   * longer be called.
330   */
331  protected abstract void runOneIteration() throws Exception;
332
333  /**
334   * Start the service.
335   *
336   * <p>By default this method does nothing.
337   */
338  protected void startUp() throws Exception {}
339
340  /**
341   * Stop the service. This is guaranteed not to run concurrently with {@link #runOneIteration}.
342   *
343   * <p>By default this method does nothing.
344   */
345  protected void shutDown() throws Exception {}
346
347  /**
348   * Returns the {@link Scheduler} object used to configure this service. This method will only be
349   * called once.
350   */
351  // TODO(cpovirk): @ForOverride
352  protected abstract Scheduler scheduler();
353
354  /**
355   * Returns the {@link ScheduledExecutorService} that will be used to execute the {@link #startUp},
356   * {@link #runOneIteration} and {@link #shutDown} methods. If this method is overridden the
357   * executor will not be {@linkplain ScheduledExecutorService#shutdown shutdown} when this service
358   * {@linkplain Service.State#TERMINATED terminates} or {@linkplain Service.State#TERMINATED
359   * fails}. Subclasses may override this method to supply a custom {@link ScheduledExecutorService}
360   * instance. This method is guaranteed to only be called once.
361   *
362   * <p>By default this returns a new {@link ScheduledExecutorService} with a single thread pool
363   * that sets the name of the thread to the {@linkplain #serviceName() service name}. Also, the
364   * pool will be {@linkplain ScheduledExecutorService#shutdown() shut down} when the service
365   * {@linkplain Service.State#TERMINATED terminates} or {@linkplain Service.State#TERMINATED
366   * fails}.
367   */
368  protected ScheduledExecutorService executor() {
369    @WeakOuter
370    class ThreadFactoryImpl implements ThreadFactory {
371      @Override
372      public Thread newThread(Runnable runnable) {
373        return MoreExecutors.newThread(serviceName(), runnable);
374      }
375    }
376    final ScheduledExecutorService executor =
377        Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl());
378    // Add a listener to shut down the executor after the service is stopped. This ensures that the
379    // JVM shutdown will not be prevented from exiting after this service has stopped or failed.
380    // Technically this listener is added after start() was called so it is a little gross, but it
381    // is called within doStart() so we know that the service cannot terminate or fail concurrently
382    // with adding this listener so it is impossible to miss an event that we are interested in.
383    addListener(
384        new Listener() {
385          @Override
386          public void terminated(State from) {
387            executor.shutdown();
388          }
389
390          @Override
391          public void failed(State from, Throwable failure) {
392            executor.shutdown();
393          }
394        },
395        directExecutor());
396    return executor;
397  }
398
399  /**
400   * Returns the name of this service. {@link AbstractScheduledService} may include the name in
401   * debugging output.
402   *
403   * @since 14.0
404   */
405  protected String serviceName() {
406    return getClass().getSimpleName();
407  }
408
409  @Override
410  public String toString() {
411    return serviceName() + " [" + state() + "]";
412  }
413
414  @Override
415  public final boolean isRunning() {
416    return delegate.isRunning();
417  }
418
419  @Override
420  public final State state() {
421    return delegate.state();
422  }
423
424  /** @since 13.0 */
425  @Override
426  public final void addListener(Listener listener, Executor executor) {
427    delegate.addListener(listener, executor);
428  }
429
430  /** @since 14.0 */
431  @Override
432  public final Throwable failureCause() {
433    return delegate.failureCause();
434  }
435
436  /** @since 15.0 */
437  @CanIgnoreReturnValue
438  @Override
439  public final Service startAsync() {
440    delegate.startAsync();
441    return this;
442  }
443
444  /** @since 15.0 */
445  @CanIgnoreReturnValue
446  @Override
447  public final Service stopAsync() {
448    delegate.stopAsync();
449    return this;
450  }
451
452  /** @since 15.0 */
453  @Override
454  public final void awaitRunning() {
455    delegate.awaitRunning();
456  }
457
458  /** @since 28.0 */
459  @Override
460  public final void awaitRunning(Duration timeout) throws TimeoutException {
461    Service.super.awaitRunning(timeout);
462  }
463
464  /** @since 15.0 */
465  @Override
466  public final void awaitRunning(long timeout, TimeUnit unit) throws TimeoutException {
467    delegate.awaitRunning(timeout, unit);
468  }
469
470  /** @since 15.0 */
471  @Override
472  public final void awaitTerminated() {
473    delegate.awaitTerminated();
474  }
475
476  /** @since 28.0 */
477  @Override
478  public final void awaitTerminated(Duration timeout) throws TimeoutException {
479    Service.super.awaitTerminated(timeout);
480  }
481
482  /** @since 15.0 */
483  @Override
484  public final void awaitTerminated(long timeout, TimeUnit unit) throws TimeoutException {
485    delegate.awaitTerminated(timeout, unit);
486  }
487
488  interface Cancellable {
489    void cancel(boolean mayInterruptIfRunning);
490
491    boolean isCancelled();
492  }
493
494  private static final class FutureAsCancellable implements Cancellable {
495    private final Future<?> delegate;
496
497    FutureAsCancellable(Future<?> delegate) {
498      this.delegate = delegate;
499    }
500
501    @Override
502    @SuppressWarnings("Interruption") // We are propagating an interrupt from a caller.
503    public void cancel(boolean mayInterruptIfRunning) {
504      delegate.cancel(mayInterruptIfRunning);
505    }
506
507    @Override
508    public boolean isCancelled() {
509      return delegate.isCancelled();
510    }
511  }
512
513  /**
514   * A {@link Scheduler} that provides a convenient way for the {@link AbstractScheduledService} to
515   * use a dynamically changing schedule. After every execution of the task, assuming it hasn't been
516   * cancelled, the {@link #getNextSchedule} method will be called.
517   *
518   * @author Luke Sandberg
519   * @since 11.0
520   */
521  public abstract static class CustomScheduler extends Scheduler {
522    /** Constructor for use by subclasses. */
523    public CustomScheduler() {}
524
525    /** A callable class that can reschedule itself using a {@link CustomScheduler}. */
526    private final class ReschedulableCallable implements Callable<@Nullable Void> {
527
528      /** The underlying task. */
529      private final Runnable wrappedRunnable;
530
531      /** The executor on which this Callable will be scheduled. */
532      private final ScheduledExecutorService executor;
533
534      /**
535       * The service that is managing this callable. This is used so that failure can be reported
536       * properly.
537       */
538      /*
539       * This reference is part of a reference cycle, which is typically something we want to avoid
540       * under j2objc -- but it is not detected by our j2objc cycle test. The cycle:
541       *
542       * - CustomScheduler.service contains an instance of ServiceDelegate. (It needs it so that it
543       *   can call notifyFailed.)
544       *
545       * - ServiceDelegate.runningTask contains an instance of ReschedulableCallable (at least in
546       *   the case that the service is using CustomScheduler). (It needs it so that it can cancel
547       *   the task and detect whether it has been cancelled.)
548       *
549       * - ReschedulableCallable has a reference back to its enclosing CustomScheduler. (It needs it
550       *   so that it can call getNextSchedule).
551       *
552       * Maybe there is a way to avoid this cycle. But we think the cycle is safe enough to ignore:
553       * Each task is retained for only as long as it is running -- so it's retained only as long as
554       * it would already be retained by the underlying executor.
555       *
556       * If the cycle test starts reporting this cycle in the future, we should add an entry to
557       * cycle_suppress_list.txt.
558       */
559      private final AbstractService service;
560
561      /**
562       * This lock is used to ensure safe and correct cancellation, it ensures that a new task is
563       * not scheduled while a cancel is ongoing. Also it protects the currentFuture variable to
564       * ensure that it is assigned atomically with being scheduled.
565       */
566      private final ReentrantLock lock = new ReentrantLock();
567
568      /** The future that represents the next execution of this task. */
569      @GuardedBy("lock")
570      @CheckForNull
571      private SupplantableFuture cancellationDelegate;
572
573      ReschedulableCallable(
574          AbstractService service, ScheduledExecutorService executor, Runnable runnable) {
575        this.wrappedRunnable = runnable;
576        this.executor = executor;
577        this.service = service;
578      }
579
580      @Override
581      @CheckForNull
582      public Void call() throws Exception {
583        wrappedRunnable.run();
584        reschedule();
585        return null;
586      }
587
588      /**
589       * Atomically reschedules this task and assigns the new future to {@link
590       * #cancellationDelegate}.
591       */
592      @CanIgnoreReturnValue
593      public Cancellable reschedule() {
594        // invoke the callback outside the lock, prevents some shenanigans.
595        Schedule schedule;
596        try {
597          schedule = CustomScheduler.this.getNextSchedule();
598        } catch (Throwable t) {
599          restoreInterruptIfIsInterruptedException(t);
600          service.notifyFailed(t);
601          return new FutureAsCancellable(immediateCancelledFuture());
602        }
603        // We reschedule ourselves with a lock held for two reasons. 1. we want to make sure that
604        // cancel calls cancel on the correct future. 2. we want to make sure that the assignment
605        // to currentFuture doesn't race with itself so that currentFuture is assigned in the
606        // correct order.
607        Throwable scheduleFailure = null;
608        Cancellable toReturn;
609        lock.lock();
610        try {
611          toReturn = initializeOrUpdateCancellationDelegate(schedule);
612        } catch (Throwable e) {
613          // Any Exception is either a RuntimeException or sneaky checked exception.
614          //
615          // If an exception is thrown by the subclass then we need to make sure that the service
616          // notices and transitions to the FAILED state. We do it by calling notifyFailed directly
617          // because the service does not monitor the state of the future so if the exception is not
618          // caught and forwarded to the service the task would stop executing but the service would
619          // have no idea.
620          // TODO(lukes): consider building everything in terms of ListenableScheduledFuture then
621          // the AbstractService could monitor the future directly. Rescheduling is still hard...
622          // but it would help with some of these lock ordering issues.
623          scheduleFailure = e;
624          toReturn = new FutureAsCancellable(immediateCancelledFuture());
625        } finally {
626          lock.unlock();
627        }
628        // Call notifyFailed outside the lock to avoid lock ordering issues.
629        if (scheduleFailure != null) {
630          service.notifyFailed(scheduleFailure);
631        }
632        return toReturn;
633      }
634
635      @GuardedBy("lock")
636      /*
637       * The GuardedBy checker warns us that we're not holding cancellationDelegate.lock. But in
638       * fact we are holding it because it is the same as this.lock, which we know we are holding,
639       * thanks to @GuardedBy above. (cancellationDelegate.lock is initialized to this.lock in the
640       * call to `new SupplantableFuture` below.)
641       */
642      @SuppressWarnings("GuardedBy")
643      private Cancellable initializeOrUpdateCancellationDelegate(Schedule schedule) {
644        if (cancellationDelegate == null) {
645          return cancellationDelegate = new SupplantableFuture(lock, submitToExecutor(schedule));
646        }
647        if (!cancellationDelegate.currentFuture.isCancelled()) {
648          cancellationDelegate.currentFuture = submitToExecutor(schedule);
649        }
650        return cancellationDelegate;
651      }
652
653      private ScheduledFuture<@Nullable Void> submitToExecutor(Schedule schedule) {
654        return executor.schedule(this, schedule.delay, schedule.unit);
655      }
656    }
657
658    /**
659     * Contains the most recently submitted {@code Future}, which may be cancelled or updated,
660     * always under a lock.
661     */
662    private static final class SupplantableFuture implements Cancellable {
663      private final ReentrantLock lock;
664
665      @GuardedBy("lock")
666      private Future<@Nullable Void> currentFuture;
667
668      SupplantableFuture(ReentrantLock lock, Future<@Nullable Void> currentFuture) {
669        this.lock = lock;
670        this.currentFuture = currentFuture;
671      }
672
673      @Override
674      @SuppressWarnings("Interruption") // We are propagating an interrupt from a caller.
675      public void cancel(boolean mayInterruptIfRunning) {
676        /*
677         * Lock to ensure that a task cannot be rescheduled while a cancel is ongoing.
678         *
679         * In theory, cancel() could execute arbitrary listeners -- bad to do while holding a lock.
680         * However, we don't expose currentFuture to users, so they can't attach listeners. And the
681         * Future might not even be a ListenableFuture, just a plain Future. That said, similar
682         * problems can exist with methods like FutureTask.done(), not to mention slow calls to
683         * Thread.interrupt() (as discussed in InterruptibleTask). At the end of the day, it's
684         * unlikely that cancel() will be slow, so we can probably get away with calling it while
685         * holding a lock. Still, it would be nice to avoid somehow.
686         */
687        lock.lock();
688        try {
689          currentFuture.cancel(mayInterruptIfRunning);
690        } finally {
691          lock.unlock();
692        }
693      }
694
695      @Override
696      public boolean isCancelled() {
697        lock.lock();
698        try {
699          return currentFuture.isCancelled();
700        } finally {
701          lock.unlock();
702        }
703      }
704    }
705
706    @Override
707    final Cancellable schedule(
708        AbstractService service, ScheduledExecutorService executor, Runnable runnable) {
709      return new ReschedulableCallable(service, executor, runnable).reschedule();
710    }
711
712    /**
713     * A value object that represents an absolute delay until a task should be invoked.
714     *
715     * @author Luke Sandberg
716     * @since 11.0
717     */
718    protected static final class Schedule {
719
720      private final long delay;
721      private final TimeUnit unit;
722
723      /**
724       * @param delay the time from now to delay execution
725       * @param unit the time unit of the delay parameter
726       */
727      public Schedule(long delay, TimeUnit unit) {
728        this.delay = delay;
729        this.unit = checkNotNull(unit);
730      }
731
732      /**
733       * @param delay the time from now to delay execution
734       * @since 31.1 (but only since 33.4.0 in the Android flavor)
735       */
736      public Schedule(Duration delay) {
737        this(toNanosSaturated(delay), NANOSECONDS);
738      }
739    }
740
741    /**
742     * Calculates the time at which to next invoke the task.
743     *
744     * <p>This is guaranteed to be called immediately after the task has completed an iteration and
745     * on the same thread as the previous execution of {@link
746     * AbstractScheduledService#runOneIteration}.
747     *
748     * @return a schedule that defines the delay before the next execution.
749     */
750    // TODO(cpovirk): @ForOverride
751    protected abstract Schedule getNextSchedule() throws Exception;
752  }
753}