001/*
002 * Copyright (C) 2007 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;
019
020import com.google.common.annotations.Beta;
021import com.google.common.annotations.GwtCompatible;
022import com.google.common.annotations.GwtIncompatible;
023import com.google.common.annotations.VisibleForTesting;
024import com.google.common.base.Supplier;
025import com.google.common.base.Throwables;
026import com.google.common.collect.Lists;
027import com.google.common.collect.Queues;
028import com.google.common.util.concurrent.ForwardingListenableFuture.SimpleForwardingListenableFuture;
029import com.google.errorprone.annotations.CanIgnoreReturnValue;
030import java.lang.reflect.InvocationTargetException;
031import java.util.Collection;
032import java.util.Collections;
033import java.util.Iterator;
034import java.util.List;
035import java.util.concurrent.BlockingQueue;
036import java.util.concurrent.Callable;
037import java.util.concurrent.Delayed;
038import java.util.concurrent.ExecutionException;
039import java.util.concurrent.Executor;
040import java.util.concurrent.ExecutorService;
041import java.util.concurrent.Executors;
042import java.util.concurrent.Future;
043import java.util.concurrent.RejectedExecutionException;
044import java.util.concurrent.ScheduledExecutorService;
045import java.util.concurrent.ScheduledFuture;
046import java.util.concurrent.ScheduledThreadPoolExecutor;
047import java.util.concurrent.ThreadFactory;
048import java.util.concurrent.ThreadPoolExecutor;
049import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
050import java.util.concurrent.TimeUnit;
051import java.util.concurrent.TimeoutException;
052import javax.annotation.concurrent.GuardedBy;
053
054/**
055 * Factory and utility methods for {@link java.util.concurrent.Executor}, {@link ExecutorService},
056 * and {@link ThreadFactory}.
057 *
058 * @author Eric Fellheimer
059 * @author Kyle Littlefield
060 * @author Justin Mahoney
061 * @since 3.0
062 */
063@GwtCompatible(emulated = true)
064public final class MoreExecutors {
065  private MoreExecutors() {}
066
067  /**
068   * Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application
069   * is complete. It does so by using daemon threads and adding a shutdown hook to wait for their
070   * completion.
071   *
072   * <p>This is mainly for fixed thread pools. See {@link Executors#newFixedThreadPool(int)}.
073   *
074   * @param executor the executor to modify to make sure it exits when the application is finished
075   * @param terminationTimeout how long to wait for the executor to finish before terminating the
076   *     JVM
077   * @param timeUnit unit of time for the time parameter
078   * @return an unmodifiable version of the input which will not hang the JVM
079   */
080  @Beta
081  @GwtIncompatible // TODO
082  public static ExecutorService getExitingExecutorService(
083      ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) {
084    return new Application().getExitingExecutorService(executor, terminationTimeout, timeUnit);
085  }
086
087  /**
088   * Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when
089   * the application is complete. It does so by using daemon threads and adding a shutdown hook to
090   * wait for their completion.
091   *
092   * <p>This is mainly for fixed thread pools. See {@link Executors#newScheduledThreadPool(int)}.
093   *
094   * @param executor the executor to modify to make sure it exits when the application is finished
095   * @param terminationTimeout how long to wait for the executor to finish before terminating the
096   *     JVM
097   * @param timeUnit unit of time for the time parameter
098   * @return an unmodifiable version of the input which will not hang the JVM
099   */
100  @Beta
101  @GwtIncompatible // TODO
102  public static ScheduledExecutorService getExitingScheduledExecutorService(
103      ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) {
104    return new Application()
105        .getExitingScheduledExecutorService(executor, terminationTimeout, timeUnit);
106  }
107
108  /**
109   * Add a shutdown hook to wait for thread completion in the given {@link ExecutorService service}.
110   * This is useful if the given service uses daemon threads, and we want to keep the JVM from
111   * exiting immediately on shutdown, instead giving these daemon threads a chance to terminate
112   * normally.
113   *
114   * @param service ExecutorService which uses daemon threads
115   * @param terminationTimeout how long to wait for the executor to finish before terminating the
116   *     JVM
117   * @param timeUnit unit of time for the time parameter
118   */
119  @Beta
120  @GwtIncompatible // TODO
121  public static void addDelayedShutdownHook(
122      ExecutorService service, long terminationTimeout, TimeUnit timeUnit) {
123    new Application().addDelayedShutdownHook(service, terminationTimeout, timeUnit);
124  }
125
126  /**
127   * Converts the given ThreadPoolExecutor into an ExecutorService that exits when the application
128   * is complete. It does so by using daemon threads and adding a shutdown hook to wait for their
129   * completion.
130   *
131   * <p>This method waits 120 seconds before continuing with JVM termination, even if the executor
132   * has not finished its work.
133   *
134   * <p>This is mainly for fixed thread pools. See {@link Executors#newFixedThreadPool(int)}.
135   *
136   * @param executor the executor to modify to make sure it exits when the application is finished
137   * @return an unmodifiable version of the input which will not hang the JVM
138   */
139  @Beta
140  @GwtIncompatible // concurrency
141  public static ExecutorService getExitingExecutorService(ThreadPoolExecutor executor) {
142    return new Application().getExitingExecutorService(executor);
143  }
144
145  /**
146   * Converts the given ScheduledThreadPoolExecutor into a ScheduledExecutorService that exits when
147   * the application is complete. It does so by using daemon threads and adding a shutdown hook to
148   * wait for their completion.
149   *
150   * <p>This method waits 120 seconds before continuing with JVM termination, even if the executor
151   * has not finished its work.
152   *
153   * <p>This is mainly for fixed thread pools. See {@link Executors#newScheduledThreadPool(int)}.
154   *
155   * @param executor the executor to modify to make sure it exits when the application is finished
156   * @return an unmodifiable version of the input which will not hang the JVM
157   */
158  @Beta
159  @GwtIncompatible // TODO
160  public static ScheduledExecutorService getExitingScheduledExecutorService(
161      ScheduledThreadPoolExecutor executor) {
162    return new Application().getExitingScheduledExecutorService(executor);
163  }
164
165  /** Represents the current application to register shutdown hooks. */
166  @GwtIncompatible // TODO
167  @VisibleForTesting
168  static class Application {
169
170    final ExecutorService getExitingExecutorService(
171        ThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) {
172      useDaemonThreadFactory(executor);
173      ExecutorService service = Executors.unconfigurableExecutorService(executor);
174      addDelayedShutdownHook(executor, terminationTimeout, timeUnit);
175      return service;
176    }
177
178    final ScheduledExecutorService getExitingScheduledExecutorService(
179        ScheduledThreadPoolExecutor executor, long terminationTimeout, TimeUnit timeUnit) {
180      useDaemonThreadFactory(executor);
181      ScheduledExecutorService service = Executors.unconfigurableScheduledExecutorService(executor);
182      addDelayedShutdownHook(executor, terminationTimeout, timeUnit);
183      return service;
184    }
185
186    final void addDelayedShutdownHook(
187        final ExecutorService service, final long terminationTimeout, final TimeUnit timeUnit) {
188      checkNotNull(service);
189      checkNotNull(timeUnit);
190      addShutdownHook(
191          MoreExecutors.newThread(
192              "DelayedShutdownHook-for-" + service,
193              new Runnable() {
194                @Override
195                public void run() {
196                  try {
197                    // We'd like to log progress and failures that may arise in the
198                    // following code, but unfortunately the behavior of logging
199                    // is undefined in shutdown hooks.
200                    // This is because the logging code installs a shutdown hook of its
201                    // own. See Cleaner class inside {@link LogManager}.
202                    service.shutdown();
203                    service.awaitTermination(terminationTimeout, timeUnit);
204                  } catch (InterruptedException ignored) {
205                    // We're shutting down anyway, so just ignore.
206                  }
207                }
208              }));
209    }
210
211    final ExecutorService getExitingExecutorService(ThreadPoolExecutor executor) {
212      return getExitingExecutorService(executor, 120, TimeUnit.SECONDS);
213    }
214
215    final ScheduledExecutorService getExitingScheduledExecutorService(
216        ScheduledThreadPoolExecutor executor) {
217      return getExitingScheduledExecutorService(executor, 120, TimeUnit.SECONDS);
218    }
219
220    @VisibleForTesting
221    void addShutdownHook(Thread hook) {
222      Runtime.getRuntime().addShutdownHook(hook);
223    }
224  }
225
226  @GwtIncompatible // TODO
227  private static void useDaemonThreadFactory(ThreadPoolExecutor executor) {
228    executor.setThreadFactory(
229        new ThreadFactoryBuilder()
230            .setDaemon(true)
231            .setThreadFactory(executor.getThreadFactory())
232            .build());
233  }
234
235  // See newDirectExecutorService javadoc for behavioral notes.
236  @GwtIncompatible // TODO
237  private static final class DirectExecutorService extends AbstractListeningExecutorService {
238    /**
239     * Lock used whenever accessing the state variables (runningTasks, shutdown) of the executor
240     */
241    private final Object lock = new Object();
242
243    /*
244     * Conceptually, these two variables describe the executor being in
245     * one of three states:
246     *   - Active: shutdown == false
247     *   - Shutdown: runningTasks > 0 and shutdown == true
248     *   - Terminated: runningTasks == 0 and shutdown == true
249     */
250    @GuardedBy("lock")
251    private int runningTasks = 0;
252
253    @GuardedBy("lock")
254    private boolean shutdown = false;
255
256    @Override
257    public void execute(Runnable command) {
258      startTask();
259      try {
260        command.run();
261      } finally {
262        endTask();
263      }
264    }
265
266    @Override
267    public boolean isShutdown() {
268      synchronized (lock) {
269        return shutdown;
270      }
271    }
272
273    @Override
274    public void shutdown() {
275      synchronized (lock) {
276        shutdown = true;
277        if (runningTasks == 0) {
278          lock.notifyAll();
279        }
280      }
281    }
282
283    // See newDirectExecutorService javadoc for unusual behavior of this method.
284    @Override
285    public List<Runnable> shutdownNow() {
286      shutdown();
287      return Collections.emptyList();
288    }
289
290    @Override
291    public boolean isTerminated() {
292      synchronized (lock) {
293        return shutdown && runningTasks == 0;
294      }
295    }
296
297    @Override
298    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
299      long nanos = unit.toNanos(timeout);
300      synchronized (lock) {
301        while (true) {
302          if (shutdown && runningTasks == 0) {
303            return true;
304          } else if (nanos <= 0) {
305            return false;
306          } else {
307            long now = System.nanoTime();
308            TimeUnit.NANOSECONDS.timedWait(lock, nanos);
309            nanos -= System.nanoTime() - now; // subtract the actual time we waited
310          }
311        }
312      }
313    }
314
315    /**
316     * Checks if the executor has been shut down and increments the running task count.
317     *
318     * @throws RejectedExecutionException if the executor has been previously shutdown
319     */
320    private void startTask() {
321      synchronized (lock) {
322        if (shutdown) {
323          throw new RejectedExecutionException("Executor already shutdown");
324        }
325        runningTasks++;
326      }
327    }
328
329    /**
330     * Decrements the running task count.
331     */
332    private void endTask() {
333      synchronized (lock) {
334        int numRunning = --runningTasks;
335        if (numRunning == 0) {
336          lock.notifyAll();
337        }
338      }
339    }
340  }
341
342  /**
343   * Creates an executor service that runs each task in the thread that invokes
344   * {@code execute/submit}, as in {@link CallerRunsPolicy} This applies both to individually
345   * submitted tasks and to collections of tasks submitted via {@code invokeAll} or
346   * {@code invokeAny}. In the latter case, tasks will run serially on the calling thread. Tasks are
347   * run to completion before a {@code Future} is returned to the caller (unless the executor has
348   * been shutdown).
349   *
350   * <p>Although all tasks are immediately executed in the thread that submitted the task, this
351   * {@code ExecutorService} imposes a small locking overhead on each task submission in order to
352   * implement shutdown and termination behavior.
353   *
354   * <p>The implementation deviates from the {@code ExecutorService} specification with regards to
355   * the {@code shutdownNow} method. First, "best-effort" with regards to canceling running tasks is
356   * implemented as "no-effort". No interrupts or other attempts are made to stop threads executing
357   * tasks. Second, the returned list will always be empty, as any submitted task is considered to
358   * have started execution. This applies also to tasks given to {@code invokeAll} or
359   * {@code invokeAny} which are pending serial execution, even the subset of the tasks that have
360   * not yet started execution. It is unclear from the {@code ExecutorService} specification if
361   * these should be included, and it's much easier to implement the interpretation that they not
362   * be. Finally, a call to {@code shutdown} or {@code shutdownNow} may result in concurrent calls
363   * to {@code invokeAll/invokeAny} throwing RejectedExecutionException, although a subset of the
364   * tasks may already have been executed.
365   *
366   * @since 18.0 (present as MoreExecutors.sameThreadExecutor() since 10.0)
367   */
368  @GwtIncompatible // TODO
369  public static ListeningExecutorService newDirectExecutorService() {
370    return new DirectExecutorService();
371  }
372
373  /**
374   * Returns an {@link Executor} that runs each task in the thread that invokes
375   * {@link Executor#execute execute}, as in {@link CallerRunsPolicy}.
376   *
377   * <p>This instance is equivalent to: <pre>   {@code
378   *   final class DirectExecutor implements Executor {
379   *     public void execute(Runnable r) {
380   *       r.run();
381   *     }
382   *   }}</pre>
383   *
384   * <p>This should be preferred to {@link #newDirectExecutorService()} because implementing the
385   * {@link ExecutorService} subinterface necessitates significant performance overhead.
386   *
387   * @since 18.0
388   */
389  public static Executor directExecutor() {
390    return DirectExecutor.INSTANCE;
391  }
392
393  /** See {@link #directExecutor} for behavioral notes. */
394  private enum DirectExecutor implements Executor {
395    INSTANCE;
396
397    @Override
398    public void execute(Runnable command) {
399      command.run();
400    }
401
402    @Override
403    public String toString() {
404      return "MoreExecutors.directExecutor()";
405    }
406  }
407
408  /**
409   * Returns an {@link Executor} that runs each task executed sequentially, such that no
410   * two tasks are running concurrently.
411   *
412   * <p>The executor uses {@code delegate} in order to {@link Executor#execute execute} each task in
413   * turn, and does not create any threads of its own.
414   *
415   * <p>After execution starts on the {@code delegate} {@link Executor}, tasks are polled and
416   * executed from the queue until there are no more tasks. The thread will not be released until
417   * there are no more tasks to run.
418   *
419   * <p>If a task is {@linkplain Thread#interrupt interrupted}, execution of subsequent tasks
420   * continues. {@code RuntimeException}s thrown by tasks are simply logged and the executor keeps
421   * trucking. If an {@code Error} is thrown, the error will propagate and execution will stop until
422   * the next time a task is submitted.
423   *
424   * @deprecated Use {@link #newSequentialExecutor}. This method is scheduled for removal in
425   *     January 2018.
426   * @since 23.1
427   */
428  @Beta
429  @Deprecated
430  @GwtIncompatible
431  public static Executor sequentialExecutor(Executor delegate) {
432    return new SequentialExecutor(delegate);
433  }
434
435  /**
436   * Returns an {@link Executor} that runs each task executed sequentially, such that no two tasks
437   * are running concurrently.
438   *
439   * <p>The executor uses {@code delegate} in order to {@link Executor#execute execute} each task in
440   * turn, and does not create any threads of its own.
441   *
442   * <p>After execution starts on the {@code delegate} {@link Executor}, tasks are polled and
443   * executed from the queue until there are no more tasks. The thread will not be released until
444   * there are no more tasks to run.
445   *
446   * <p>If a task is {@linkplain Thread#interrupt interrupted}, execution of subsequent tasks
447   * continues. {@code RuntimeException}s thrown by tasks are simply logged and the executor keeps
448   * trucking. If an {@code Error} is thrown, the error will propagate and execution will stop until
449   * the next time a task is submitted.
450   *
451   * @since 23.3 (since 23.1 as {@link #sequentialExecutor(Executor)})
452   */
453  @Beta
454  @GwtIncompatible
455  public static Executor newSequentialExecutor(Executor delegate) {
456    return new SequentialExecutor(delegate);
457  }
458
459  /**
460   * Creates an {@link ExecutorService} whose {@code submit} and {@code
461   * invokeAll} methods submit {@link ListenableFutureTask} instances to the given delegate
462   * executor. Those methods, as well as {@code execute} and {@code invokeAny}, are implemented in
463   * terms of calls to {@code
464   * delegate.execute}. All other methods are forwarded unchanged to the delegate. This implies that
465   * the returned {@code ListeningExecutorService} never calls the delegate's {@code submit},
466   * {@code invokeAll}, and {@code
467   * invokeAny} methods, so any special handling of tasks must be implemented in the delegate's
468   * {@code execute} method or by wrapping the returned {@code
469   * ListeningExecutorService}.
470   *
471   * <p>If the delegate executor was already an instance of {@code
472   * ListeningExecutorService}, it is returned untouched, and the rest of this documentation does
473   * not apply.
474   *
475   * @since 10.0
476   */
477  @GwtIncompatible // TODO
478  public static ListeningExecutorService listeningDecorator(ExecutorService delegate) {
479    return (delegate instanceof ListeningExecutorService)
480        ? (ListeningExecutorService) delegate
481        : (delegate instanceof ScheduledExecutorService)
482            ? new ScheduledListeningDecorator((ScheduledExecutorService) delegate)
483            : new ListeningDecorator(delegate);
484  }
485
486  /**
487   * Creates a {@link ScheduledExecutorService} whose {@code submit} and {@code invokeAll} methods
488   * submit {@link ListenableFutureTask} instances to the given delegate executor. Those methods, as
489   * well as {@code execute} and {@code invokeAny}, are implemented in terms of calls to {@code
490   * delegate.execute}. All other methods are forwarded unchanged to the delegate. This implies that
491   * the returned {@code ListeningScheduledExecutorService} never calls the delegate's {@code
492   * submit}, {@code invokeAll}, and {@code invokeAny} methods, so any special handling of tasks
493   * must be implemented in the delegate's {@code execute} method or by wrapping the returned {@code
494   * ListeningScheduledExecutorService}.
495   *
496   * <p>If the delegate executor was already an instance of {@code
497   * ListeningScheduledExecutorService}, it is returned untouched, and the rest of this
498   * documentation does not apply.
499   *
500   * @since 10.0
501   */
502  @GwtIncompatible // TODO
503  public static ListeningScheduledExecutorService listeningDecorator(
504      ScheduledExecutorService delegate) {
505    return (delegate instanceof ListeningScheduledExecutorService)
506        ? (ListeningScheduledExecutorService) delegate
507        : new ScheduledListeningDecorator(delegate);
508  }
509
510  @GwtIncompatible // TODO
511  private static class ListeningDecorator extends AbstractListeningExecutorService {
512    private final ExecutorService delegate;
513
514    ListeningDecorator(ExecutorService delegate) {
515      this.delegate = checkNotNull(delegate);
516    }
517
518    @Override
519    public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
520      return delegate.awaitTermination(timeout, unit);
521    }
522
523    @Override
524    public final boolean isShutdown() {
525      return delegate.isShutdown();
526    }
527
528    @Override
529    public final boolean isTerminated() {
530      return delegate.isTerminated();
531    }
532
533    @Override
534    public final void shutdown() {
535      delegate.shutdown();
536    }
537
538    @Override
539    public final List<Runnable> shutdownNow() {
540      return delegate.shutdownNow();
541    }
542
543    @Override
544    public final void execute(Runnable command) {
545      delegate.execute(command);
546    }
547  }
548
549  @GwtIncompatible // TODO
550  private static final class ScheduledListeningDecorator extends ListeningDecorator
551      implements ListeningScheduledExecutorService {
552    @SuppressWarnings("hiding")
553    final ScheduledExecutorService delegate;
554
555    ScheduledListeningDecorator(ScheduledExecutorService delegate) {
556      super(delegate);
557      this.delegate = checkNotNull(delegate);
558    }
559
560    @Override
561    public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
562      TrustedListenableFutureTask<Void> task = TrustedListenableFutureTask.create(command, null);
563      ScheduledFuture<?> scheduled = delegate.schedule(task, delay, unit);
564      return new ListenableScheduledTask<>(task, scheduled);
565    }
566
567    @Override
568    public <V> ListenableScheduledFuture<V> schedule(
569        Callable<V> callable, long delay, TimeUnit unit) {
570      TrustedListenableFutureTask<V> task = TrustedListenableFutureTask.create(callable);
571      ScheduledFuture<?> scheduled = delegate.schedule(task, delay, unit);
572      return new ListenableScheduledTask<V>(task, scheduled);
573    }
574
575    @Override
576    public ListenableScheduledFuture<?> scheduleAtFixedRate(
577        Runnable command, long initialDelay, long period, TimeUnit unit) {
578      NeverSuccessfulListenableFutureTask task = new NeverSuccessfulListenableFutureTask(command);
579      ScheduledFuture<?> scheduled = delegate.scheduleAtFixedRate(task, initialDelay, period, unit);
580      return new ListenableScheduledTask<>(task, scheduled);
581    }
582
583    @Override
584    public ListenableScheduledFuture<?> scheduleWithFixedDelay(
585        Runnable command, long initialDelay, long delay, TimeUnit unit) {
586      NeverSuccessfulListenableFutureTask task = new NeverSuccessfulListenableFutureTask(command);
587      ScheduledFuture<?> scheduled =
588          delegate.scheduleWithFixedDelay(task, initialDelay, delay, unit);
589      return new ListenableScheduledTask<>(task, scheduled);
590    }
591
592    private static final class ListenableScheduledTask<V>
593        extends SimpleForwardingListenableFuture<V> implements ListenableScheduledFuture<V> {
594
595      private final ScheduledFuture<?> scheduledDelegate;
596
597      public ListenableScheduledTask(
598          ListenableFuture<V> listenableDelegate, ScheduledFuture<?> scheduledDelegate) {
599        super(listenableDelegate);
600        this.scheduledDelegate = scheduledDelegate;
601      }
602
603      @Override
604      public boolean cancel(boolean mayInterruptIfRunning) {
605        boolean cancelled = super.cancel(mayInterruptIfRunning);
606        if (cancelled) {
607          // Unless it is cancelled, the delegate may continue being scheduled
608          scheduledDelegate.cancel(mayInterruptIfRunning);
609
610          // TODO(user): Cancel "this" if "scheduledDelegate" is cancelled.
611        }
612        return cancelled;
613      }
614
615      @Override
616      public long getDelay(TimeUnit unit) {
617        return scheduledDelegate.getDelay(unit);
618      }
619
620      @Override
621      public int compareTo(Delayed other) {
622        return scheduledDelegate.compareTo(other);
623      }
624    }
625
626    @GwtIncompatible // TODO
627    private static final class NeverSuccessfulListenableFutureTask extends AbstractFuture<Void>
628        implements Runnable {
629      private final Runnable delegate;
630
631      public NeverSuccessfulListenableFutureTask(Runnable delegate) {
632        this.delegate = checkNotNull(delegate);
633      }
634
635      @Override
636      public void run() {
637        try {
638          delegate.run();
639        } catch (Throwable t) {
640          setException(t);
641          throw Throwables.propagate(t);
642        }
643      }
644    }
645  }
646
647  /*
648   * This following method is a modified version of one found in
649   * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/test/tck/AbstractExecutorServiceTest.java?revision=1.30
650   * which contained the following notice:
651   *
652   * Written by Doug Lea with assistance from members of JCP JSR-166 Expert Group and released to
653   * the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/
654   *
655   * Other contributors include Andrew Wright, Jeffrey Hayes, Pat Fisher, Mike Judd.
656   */
657
658  /**
659   * An implementation of {@link ExecutorService#invokeAny} for {@link ListeningExecutorService}
660   * implementations.
661   */
662  @GwtIncompatible static <T> T invokeAnyImpl(
663      ListeningExecutorService executorService,
664      Collection<? extends Callable<T>> tasks,
665      boolean timed,
666      long timeout,
667      TimeUnit unit)
668      throws InterruptedException, ExecutionException, TimeoutException {
669    checkNotNull(executorService);
670    checkNotNull(unit);
671    int ntasks = tasks.size();
672    checkArgument(ntasks > 0);
673    List<Future<T>> futures = Lists.newArrayListWithCapacity(ntasks);
674    BlockingQueue<Future<T>> futureQueue = Queues.newLinkedBlockingQueue();
675    long timeoutNanos = unit.toNanos(timeout);
676
677    // For efficiency, especially in executors with limited
678    // parallelism, check to see if previously submitted tasks are
679    // done before submitting more of them. This interleaving
680    // plus the exception mechanics account for messiness of main
681    // loop.
682
683    try {
684      // Record exceptions so that if we fail to obtain any
685      // result, we can throw the last exception we got.
686      ExecutionException ee = null;
687      long lastTime = timed ? System.nanoTime() : 0;
688      Iterator<? extends Callable<T>> it = tasks.iterator();
689
690      futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue));
691      --ntasks;
692      int active = 1;
693
694      while (true) {
695        Future<T> f = futureQueue.poll();
696        if (f == null) {
697          if (ntasks > 0) {
698            --ntasks;
699            futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue));
700            ++active;
701          } else if (active == 0) {
702            break;
703          } else if (timed) {
704            f = futureQueue.poll(timeoutNanos, TimeUnit.NANOSECONDS);
705            if (f == null) {
706              throw new TimeoutException();
707            }
708            long now = System.nanoTime();
709            timeoutNanos -= now - lastTime;
710            lastTime = now;
711          } else {
712            f = futureQueue.take();
713          }
714        }
715        if (f != null) {
716          --active;
717          try {
718            return f.get();
719          } catch (ExecutionException eex) {
720            ee = eex;
721          } catch (RuntimeException rex) {
722            ee = new ExecutionException(rex);
723          }
724        }
725      }
726
727      if (ee == null) {
728        ee = new ExecutionException(null);
729      }
730      throw ee;
731    } finally {
732      for (Future<T> f : futures) {
733        f.cancel(true);
734      }
735    }
736  }
737
738  /**
739   * Submits the task and adds a listener that adds the future to {@code queue} when it completes.
740   */
741  @GwtIncompatible // TODO
742  private static <T> ListenableFuture<T> submitAndAddQueueListener(
743      ListeningExecutorService executorService,
744      Callable<T> task,
745      final BlockingQueue<Future<T>> queue) {
746    final ListenableFuture<T> future = executorService.submit(task);
747    future.addListener(
748        new Runnable() {
749          @Override
750          public void run() {
751            queue.add(future);
752          }
753        },
754        directExecutor());
755    return future;
756  }
757
758  /**
759   * Returns a default thread factory used to create new threads.
760   *
761   * <p>On AppEngine, returns {@code ThreadManager.currentRequestThreadFactory()}. Otherwise,
762   * returns {@link Executors#defaultThreadFactory()}.
763   *
764   * @since 14.0
765   */
766  @Beta
767  @GwtIncompatible // concurrency
768  public static ThreadFactory platformThreadFactory() {
769    if (!isAppEngine()) {
770      return Executors.defaultThreadFactory();
771    }
772    try {
773      return (ThreadFactory)
774          Class.forName("com.google.appengine.api.ThreadManager")
775              .getMethod("currentRequestThreadFactory")
776              .invoke(null);
777    } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) {
778      throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
779    } catch (InvocationTargetException e) {
780      throw Throwables.propagate(e.getCause());
781    }
782  }
783
784  @GwtIncompatible // TODO
785  private static boolean isAppEngine() {
786    if (System.getProperty("com.google.appengine.runtime.environment") == null) {
787      return false;
788    }
789    try {
790      // If the current environment is null, we're not inside AppEngine.
791      return Class.forName("com.google.apphosting.api.ApiProxy")
792              .getMethod("getCurrentEnvironment")
793              .invoke(null)
794          != null;
795    } catch (ClassNotFoundException e) {
796      // If ApiProxy doesn't exist, we're not on AppEngine at all.
797      return false;
798    } catch (InvocationTargetException e) {
799      // If ApiProxy throws an exception, we're not in a proper AppEngine environment.
800      return false;
801    } catch (IllegalAccessException e) {
802      // If the method isn't accessible, we're not on a supported version of AppEngine;
803      return false;
804    } catch (NoSuchMethodException e) {
805      // If the method doesn't exist, we're not on a supported version of AppEngine;
806      return false;
807    }
808  }
809
810  /**
811   * Creates a thread using {@link #platformThreadFactory}, and sets its name to {@code name} unless
812   * changing the name is forbidden by the security manager.
813   */
814  @GwtIncompatible // concurrency
815  static Thread newThread(String name, Runnable runnable) {
816    checkNotNull(name);
817    checkNotNull(runnable);
818    Thread result = platformThreadFactory().newThread(runnable);
819    try {
820      result.setName(name);
821    } catch (SecurityException e) {
822      // OK if we can't set the name in this environment.
823    }
824    return result;
825  }
826
827  // TODO(lukes): provide overloads for ListeningExecutorService? ListeningScheduledExecutorService?
828  // TODO(lukes): provide overloads that take constant strings? Function<Runnable, String>s to
829  // calculate names?
830
831  /**
832   * Creates an {@link Executor} that renames the {@link Thread threads} that its tasks run in.
833   *
834   * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed
835   * right before each task is run. The renaming is best effort, if a {@link SecurityManager}
836   * prevents the renaming then it will be skipped but the tasks will still execute.
837   *
838   *
839   * @param executor The executor to decorate
840   * @param nameSupplier The source of names for each task
841   */
842  @GwtIncompatible // concurrency
843  static Executor renamingDecorator(final Executor executor, final Supplier<String> nameSupplier) {
844    checkNotNull(executor);
845    checkNotNull(nameSupplier);
846    if (isAppEngine()) {
847      // AppEngine doesn't support thread renaming, so don't even try
848      return executor;
849    }
850    return new Executor() {
851      @Override
852      public void execute(Runnable command) {
853        executor.execute(Callables.threadRenaming(command, nameSupplier));
854      }
855    };
856  }
857
858  /**
859   * Creates an {@link ExecutorService} that renames the {@link Thread threads} that its tasks run
860   * in.
861   *
862   * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed
863   * right before each task is run. The renaming is best effort, if a {@link SecurityManager}
864   * prevents the renaming then it will be skipped but the tasks will still execute.
865   *
866   *
867   * @param service The executor to decorate
868   * @param nameSupplier The source of names for each task
869   */
870  @GwtIncompatible // concurrency
871  static ExecutorService renamingDecorator(
872      final ExecutorService service, final Supplier<String> nameSupplier) {
873    checkNotNull(service);
874    checkNotNull(nameSupplier);
875    if (isAppEngine()) {
876      // AppEngine doesn't support thread renaming, so don't even try.
877      return service;
878    }
879    return new WrappingExecutorService(service) {
880      @Override
881      protected <T> Callable<T> wrapTask(Callable<T> callable) {
882        return Callables.threadRenaming(callable, nameSupplier);
883      }
884
885      @Override
886      protected Runnable wrapTask(Runnable command) {
887        return Callables.threadRenaming(command, nameSupplier);
888      }
889    };
890  }
891
892  /**
893   * Creates a {@link ScheduledExecutorService} that renames the {@link Thread threads} that its
894   * tasks run in.
895   *
896   * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed
897   * right before each task is run. The renaming is best effort, if a {@link SecurityManager}
898   * prevents the renaming then it will be skipped but the tasks will still execute.
899   *
900   *
901   * @param service The executor to decorate
902   * @param nameSupplier The source of names for each task
903   */
904  @GwtIncompatible // concurrency
905  static ScheduledExecutorService renamingDecorator(
906      final ScheduledExecutorService service, final Supplier<String> nameSupplier) {
907    checkNotNull(service);
908    checkNotNull(nameSupplier);
909    if (isAppEngine()) {
910      // AppEngine doesn't support thread renaming, so don't even try.
911      return service;
912    }
913    return new WrappingScheduledExecutorService(service) {
914      @Override
915      protected <T> Callable<T> wrapTask(Callable<T> callable) {
916        return Callables.threadRenaming(callable, nameSupplier);
917      }
918
919      @Override
920      protected Runnable wrapTask(Runnable command) {
921        return Callables.threadRenaming(command, nameSupplier);
922      }
923    };
924  }
925
926  /**
927   * Shuts down the given executor service gradually, first disabling new submissions and later, if
928   * necessary, cancelling remaining tasks.
929   *
930   * <p>The method takes the following steps:
931   * <ol>
932   * <li>calls {@link ExecutorService#shutdown()}, disabling acceptance of new submitted tasks.
933   * <li>awaits executor service termination for half of the specified timeout.
934   * <li>if the timeout expires, it calls {@link ExecutorService#shutdownNow()}, cancelling pending
935   * tasks and interrupting running tasks.
936   * <li>awaits executor service termination for the other half of the specified timeout.
937   * </ol>
938   *
939   * <p>If, at any step of the process, the calling thread is interrupted, the method calls
940   * {@link ExecutorService#shutdownNow()} and returns.
941   *
942   * @param service the {@code ExecutorService} to shut down
943   * @param timeout the maximum time to wait for the {@code ExecutorService} to terminate
944   * @param unit the time unit of the timeout argument
945   * @return {@code true} if the {@code ExecutorService} was terminated successfully, {@code false}
946   *     if the call timed out or was interrupted
947   * @since 17.0
948   */
949  @Beta
950  @CanIgnoreReturnValue
951  @GwtIncompatible // concurrency
952  public static boolean shutdownAndAwaitTermination(
953      ExecutorService service, long timeout, TimeUnit unit) {
954    long halfTimeoutNanos = unit.toNanos(timeout) / 2;
955    // Disable new tasks from being submitted
956    service.shutdown();
957    try {
958      // Wait for half the duration of the timeout for existing tasks to terminate
959      if (!service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS)) {
960        // Cancel currently executing tasks
961        service.shutdownNow();
962        // Wait the other half of the timeout for tasks to respond to being cancelled
963        service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS);
964      }
965    } catch (InterruptedException ie) {
966      // Preserve interrupt status
967      Thread.currentThread().interrupt();
968      // (Re-)Cancel if current thread also interrupted
969      service.shutdownNow();
970    }
971    return service.isTerminated();
972  }
973
974  /**
975   * Returns an Executor that will propagate {@link RejectedExecutionException} from the delegate
976   * executor to the given {@code future}.
977   *
978   * <p>Note, the returned executor can only be used once.
979   */
980  static Executor rejectionPropagatingExecutor(
981      final Executor delegate, final AbstractFuture<?> future) {
982    checkNotNull(delegate);
983    checkNotNull(future);
984    if (delegate == directExecutor()) {
985      // directExecutor() cannot throw RejectedExecutionException
986      return delegate;
987    }
988    return new Executor() {
989      boolean thrownFromDelegate = true;
990
991      @Override
992      public void execute(final Runnable command) {
993        try {
994          delegate.execute(
995              new Runnable() {
996                @Override
997                public void run() {
998                  thrownFromDelegate = false;
999                  command.run();
1000                }
1001              });
1002        } catch (RejectedExecutionException e) {
1003          if (thrownFromDelegate) {
1004            // wrap exception?
1005            future.setException(e);
1006          }
1007          // otherwise it must have been thrown from a transitive call and the delegate runnable
1008          // should have handled it.
1009        }
1010      }
1011    };
1012  }
1013}