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(service, 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(service, 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  /**
236   * Creates an executor service that runs each task in the thread that invokes
237   * {@code execute/submit}, as in {@link CallerRunsPolicy}. This applies both to individually
238   * submitted tasks and to collections of tasks submitted via {@code invokeAll} or
239   * {@code invokeAny}. In the latter case, tasks will run serially on the calling thread. Tasks are
240   * run to completion before a {@code Future} is returned to the caller (unless the executor has
241   * been shutdown).
242   *
243   * <p>Although all tasks are immediately executed in the thread that submitted the task, this
244   * {@code ExecutorService} imposes a small locking overhead on each task submission in order to
245   * implement shutdown and termination behavior.
246   *
247   * <p>The implementation deviates from the {@code ExecutorService} specification with regards to
248   * the {@code shutdownNow} method. First, "best-effort" with regards to canceling running tasks is
249   * implemented as "no-effort". No interrupts or other attempts are made to stop threads executing
250   * tasks. Second, the returned list will always be empty, as any submitted task is considered to
251   * have started execution. This applies also to tasks given to {@code invokeAll} or
252   * {@code invokeAny} which are pending serial execution, even the subset of the tasks that have
253   * not yet started execution. It is unclear from the {@code ExecutorService} specification if
254   * these should be included, and it's much easier to implement the interpretation that they not
255   * be. Finally, a call to {@code shutdown} or {@code shutdownNow} may result in concurrent calls
256   * to {@code invokeAll/invokeAny} throwing RejectedExecutionException, although a subset of the
257   * tasks may already have been executed.
258   *
259   * @since 10.0 (<a href="https://github.com/google/guava/wiki/Compatibility">mostly
260   *     source-compatible</a> since 3.0)
261   * @deprecated Use {@link #directExecutor()} if you only require an {@link Executor} and
262   *     {@link #newDirectExecutorService()} if you need a {@link ListeningExecutorService}. This
263   *     method will be removed in Guava 21.0.
264   */
265  @Deprecated
266  @GwtIncompatible
267  public static ListeningExecutorService sameThreadExecutor() {
268    return new DirectExecutorService();
269  }
270
271  // See newDirectExecutorService javadoc for behavioral notes.
272  @GwtIncompatible // TODO
273  private static final class DirectExecutorService extends AbstractListeningExecutorService {
274    /**
275     * Lock used whenever accessing the state variables (runningTasks, shutdown) of the executor
276     */
277    private final Object lock = new Object();
278
279    /*
280     * Conceptually, these two variables describe the executor being in
281     * one of three states:
282     *   - Active: shutdown == false
283     *   - Shutdown: runningTasks > 0 and shutdown == true
284     *   - Terminated: runningTasks == 0 and shutdown == true
285     */
286    @GuardedBy("lock")
287    private int runningTasks = 0;
288
289    @GuardedBy("lock")
290    private boolean shutdown = false;
291
292    @Override
293    public void execute(Runnable command) {
294      startTask();
295      try {
296        command.run();
297      } finally {
298        endTask();
299      }
300    }
301
302    @Override
303    public boolean isShutdown() {
304      synchronized (lock) {
305        return shutdown;
306      }
307    }
308
309    @Override
310    public void shutdown() {
311      synchronized (lock) {
312        shutdown = true;
313        if (runningTasks == 0) {
314          lock.notifyAll();
315        }
316      }
317    }
318
319    // See newDirectExecutorService javadoc for unusual behavior of this method.
320    @Override
321    public List<Runnable> shutdownNow() {
322      shutdown();
323      return Collections.emptyList();
324    }
325
326    @Override
327    public boolean isTerminated() {
328      synchronized (lock) {
329        return shutdown && runningTasks == 0;
330      }
331    }
332
333    @Override
334    public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
335      long nanos = unit.toNanos(timeout);
336      synchronized (lock) {
337        while (true) {
338          if (shutdown && runningTasks == 0) {
339            return true;
340          } else if (nanos <= 0) {
341            return false;
342          } else {
343            long now = System.nanoTime();
344            TimeUnit.NANOSECONDS.timedWait(lock, nanos);
345            nanos -= System.nanoTime() - now; // subtract the actual time we waited
346          }
347        }
348      }
349    }
350
351    /**
352     * Checks if the executor has been shut down and increments the running task count.
353     *
354     * @throws RejectedExecutionException if the executor has been previously shutdown
355     */
356    private void startTask() {
357      synchronized (lock) {
358        if (shutdown) {
359          throw new RejectedExecutionException("Executor already shutdown");
360        }
361        runningTasks++;
362      }
363    }
364
365    /**
366     * Decrements the running task count.
367     */
368    private void endTask() {
369      synchronized (lock) {
370        int numRunning = --runningTasks;
371        if (numRunning == 0) {
372          lock.notifyAll();
373        }
374      }
375    }
376  }
377
378  /**
379   * Creates an executor service that runs each task in the thread that invokes
380   * {@code execute/submit}, as in {@link CallerRunsPolicy} This applies both to individually
381   * submitted tasks and to collections of tasks submitted via {@code invokeAll} or
382   * {@code invokeAny}. In the latter case, tasks will run serially on the calling thread. Tasks are
383   * run to completion before a {@code Future} is returned to the caller (unless the executor has
384   * been shutdown).
385   *
386   * <p>Although all tasks are immediately executed in the thread that submitted the task, this
387   * {@code ExecutorService} imposes a small locking overhead on each task submission in order to
388   * implement shutdown and termination behavior.
389   *
390   * <p>The implementation deviates from the {@code ExecutorService} specification with regards to
391   * the {@code shutdownNow} method. First, "best-effort" with regards to canceling running tasks is
392   * implemented as "no-effort". No interrupts or other attempts are made to stop threads executing
393   * tasks. Second, the returned list will always be empty, as any submitted task is considered to
394   * have started execution. This applies also to tasks given to {@code invokeAll} or
395   * {@code invokeAny} which are pending serial execution, even the subset of the tasks that have
396   * not yet started execution. It is unclear from the {@code ExecutorService} specification if
397   * these should be included, and it's much easier to implement the interpretation that they not
398   * be. Finally, a call to {@code shutdown} or {@code shutdownNow} may result in concurrent calls
399   * to {@code invokeAll/invokeAny} throwing RejectedExecutionException, although a subset of the
400   * tasks may already have been executed.
401   *
402   * @since 18.0 (present as MoreExecutors.sameThreadExecutor() since 10.0)
403   */
404  @GwtIncompatible // TODO
405  public static ListeningExecutorService newDirectExecutorService() {
406    return new DirectExecutorService();
407  }
408
409  /**
410   * Returns an {@link Executor} that runs each task in the thread that invokes
411   * {@link Executor#execute execute}, as in {@link CallerRunsPolicy}.
412   *
413   * <p>This instance is equivalent to: <pre>   {@code
414   *   final class DirectExecutor implements Executor {
415   *     public void execute(Runnable r) {
416   *       r.run();
417   *     }
418   *   }}</pre>
419   *
420   * <p>This should be preferred to {@link #newDirectExecutorService()} because implementing the
421   * {@link ExecutorService} subinterface necessitates significant performance overhead.
422   *
423   * @since 18.0
424   */
425  public static Executor directExecutor() {
426    return DirectExecutor.INSTANCE;
427  }
428
429  /** See {@link #directExecutor} for behavioral notes. */
430  private enum DirectExecutor implements Executor {
431    INSTANCE;
432
433    @Override
434    public void execute(Runnable command) {
435      command.run();
436    }
437
438    @Override
439    public String toString() {
440      return "MoreExecutors.directExecutor()";
441    }
442  }
443
444  /**
445   * Creates an {@link ExecutorService} whose {@code submit} and {@code
446   * invokeAll} methods submit {@link ListenableFutureTask} instances to the given delegate
447   * executor. Those methods, as well as {@code execute} and {@code invokeAny}, are implemented in
448   * terms of calls to {@code
449   * delegate.execute}. All other methods are forwarded unchanged to the delegate. This implies that
450   * the returned {@code ListeningExecutorService} never calls the delegate's {@code submit},
451   * {@code invokeAll}, and {@code
452   * invokeAny} methods, so any special handling of tasks must be implemented in the delegate's
453   * {@code execute} method or by wrapping the returned {@code
454   * ListeningExecutorService}.
455   *
456   * <p>If the delegate executor was already an instance of {@code
457   * ListeningExecutorService}, it is returned untouched, and the rest of this documentation does
458   * not apply.
459   *
460   * @since 10.0
461   */
462  @GwtIncompatible // TODO
463  public static ListeningExecutorService listeningDecorator(ExecutorService delegate) {
464    return (delegate instanceof ListeningExecutorService)
465        ? (ListeningExecutorService) delegate
466        : (delegate instanceof ScheduledExecutorService)
467            ? new ScheduledListeningDecorator((ScheduledExecutorService) delegate)
468            : new ListeningDecorator(delegate);
469  }
470
471  /**
472   * Creates a {@link ScheduledExecutorService} whose {@code submit} and {@code invokeAll} methods
473   * submit {@link ListenableFutureTask} instances to the given delegate executor. Those methods, as
474   * well as {@code execute} and {@code invokeAny}, are implemented in terms of calls to {@code
475   * delegate.execute}. All other methods are forwarded unchanged to the delegate. This implies that
476   * the returned {@code ListeningScheduledExecutorService} never calls the delegate's {@code
477   * submit}, {@code invokeAll}, and {@code invokeAny} methods, so any special handling of tasks
478   * must be implemented in the delegate's {@code execute} method or by wrapping the returned {@code
479   * ListeningScheduledExecutorService}.
480   *
481   * <p>If the delegate executor was already an instance of {@code
482   * ListeningScheduledExecutorService}, it is returned untouched, and the rest of this
483   * documentation does not apply.
484   *
485   * @since 10.0
486   */
487  @GwtIncompatible // TODO
488  public static ListeningScheduledExecutorService listeningDecorator(
489      ScheduledExecutorService delegate) {
490    return (delegate instanceof ListeningScheduledExecutorService)
491        ? (ListeningScheduledExecutorService) delegate
492        : new ScheduledListeningDecorator(delegate);
493  }
494
495  @GwtIncompatible // TODO
496  private static class ListeningDecorator extends AbstractListeningExecutorService {
497    private final ExecutorService delegate;
498
499    ListeningDecorator(ExecutorService delegate) {
500      this.delegate = checkNotNull(delegate);
501    }
502
503    @Override
504    public final boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
505      return delegate.awaitTermination(timeout, unit);
506    }
507
508    @Override
509    public final boolean isShutdown() {
510      return delegate.isShutdown();
511    }
512
513    @Override
514    public final boolean isTerminated() {
515      return delegate.isTerminated();
516    }
517
518    @Override
519    public final void shutdown() {
520      delegate.shutdown();
521    }
522
523    @Override
524    public final List<Runnable> shutdownNow() {
525      return delegate.shutdownNow();
526    }
527
528    @Override
529    public final void execute(Runnable command) {
530      delegate.execute(command);
531    }
532  }
533
534  @GwtIncompatible // TODO
535  private static final class ScheduledListeningDecorator extends ListeningDecorator
536      implements ListeningScheduledExecutorService {
537    @SuppressWarnings("hiding")
538    final ScheduledExecutorService delegate;
539
540    ScheduledListeningDecorator(ScheduledExecutorService delegate) {
541      super(delegate);
542      this.delegate = checkNotNull(delegate);
543    }
544
545    @Override
546    public ListenableScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
547      TrustedListenableFutureTask<Void> task = TrustedListenableFutureTask.create(command, null);
548      ScheduledFuture<?> scheduled = delegate.schedule(task, delay, unit);
549      return new ListenableScheduledTask<Void>(task, scheduled);
550    }
551
552    @Override
553    public <V> ListenableScheduledFuture<V> schedule(
554        Callable<V> callable, long delay, TimeUnit unit) {
555      TrustedListenableFutureTask<V> task = TrustedListenableFutureTask.create(callable);
556      ScheduledFuture<?> scheduled = delegate.schedule(task, delay, unit);
557      return new ListenableScheduledTask<V>(task, scheduled);
558    }
559
560    @Override
561    public ListenableScheduledFuture<?> scheduleAtFixedRate(
562        Runnable command, long initialDelay, long period, TimeUnit unit) {
563      NeverSuccessfulListenableFutureTask task = new NeverSuccessfulListenableFutureTask(command);
564      ScheduledFuture<?> scheduled = delegate.scheduleAtFixedRate(task, initialDelay, period, unit);
565      return new ListenableScheduledTask<Void>(task, scheduled);
566    }
567
568    @Override
569    public ListenableScheduledFuture<?> scheduleWithFixedDelay(
570        Runnable command, long initialDelay, long delay, TimeUnit unit) {
571      NeverSuccessfulListenableFutureTask task = new NeverSuccessfulListenableFutureTask(command);
572      ScheduledFuture<?> scheduled =
573          delegate.scheduleWithFixedDelay(task, initialDelay, delay, unit);
574      return new ListenableScheduledTask<Void>(task, scheduled);
575    }
576
577    private static final class ListenableScheduledTask<V>
578        extends SimpleForwardingListenableFuture<V> implements ListenableScheduledFuture<V> {
579
580      private final ScheduledFuture<?> scheduledDelegate;
581
582      public ListenableScheduledTask(
583          ListenableFuture<V> listenableDelegate, ScheduledFuture<?> scheduledDelegate) {
584        super(listenableDelegate);
585        this.scheduledDelegate = scheduledDelegate;
586      }
587
588      @Override
589      public boolean cancel(boolean mayInterruptIfRunning) {
590        boolean cancelled = super.cancel(mayInterruptIfRunning);
591        if (cancelled) {
592          // Unless it is cancelled, the delegate may continue being scheduled
593          scheduledDelegate.cancel(mayInterruptIfRunning);
594
595          // TODO(user): Cancel "this" if "scheduledDelegate" is cancelled.
596        }
597        return cancelled;
598      }
599
600      @Override
601      public long getDelay(TimeUnit unit) {
602        return scheduledDelegate.getDelay(unit);
603      }
604
605      @Override
606      public int compareTo(Delayed other) {
607        return scheduledDelegate.compareTo(other);
608      }
609    }
610
611    @GwtIncompatible // TODO
612    private static final class NeverSuccessfulListenableFutureTask extends AbstractFuture<Void>
613        implements Runnable {
614      private final Runnable delegate;
615
616      public NeverSuccessfulListenableFutureTask(Runnable delegate) {
617        this.delegate = checkNotNull(delegate);
618      }
619
620      @Override
621      public void run() {
622        try {
623          delegate.run();
624        } catch (Throwable t) {
625          setException(t);
626          throw Throwables.propagate(t);
627        }
628      }
629    }
630  }
631
632  /*
633   * This following method is a modified version of one found in
634   * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/test/tck/AbstractExecutorServiceTest.java?revision=1.30
635   * which contained the following notice:
636   *
637   * Written by Doug Lea with assistance from members of JCP JSR-166 Expert Group and released to
638   * the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/
639   *
640   * Other contributors include Andrew Wright, Jeffrey Hayes, Pat Fisher, Mike Judd.
641   */
642
643  /**
644   * An implementation of {@link ExecutorService#invokeAny} for {@link ListeningExecutorService}
645   * implementations.
646   */
647  @GwtIncompatible static <T> T invokeAnyImpl(
648      ListeningExecutorService executorService,
649      Collection<? extends Callable<T>> tasks,
650      boolean timed,
651      long timeout,
652      TimeUnit unit)
653      throws InterruptedException, ExecutionException, TimeoutException {
654    checkNotNull(executorService);
655    checkNotNull(unit);
656    int ntasks = tasks.size();
657    checkArgument(ntasks > 0);
658    List<Future<T>> futures = Lists.newArrayListWithCapacity(ntasks);
659    BlockingQueue<Future<T>> futureQueue = Queues.newLinkedBlockingQueue();
660    long timeoutNanos = unit.toNanos(timeout);
661
662    // For efficiency, especially in executors with limited
663    // parallelism, check to see if previously submitted tasks are
664    // done before submitting more of them. This interleaving
665    // plus the exception mechanics account for messiness of main
666    // loop.
667
668    try {
669      // Record exceptions so that if we fail to obtain any
670      // result, we can throw the last exception we got.
671      ExecutionException ee = null;
672      long lastTime = timed ? System.nanoTime() : 0;
673      Iterator<? extends Callable<T>> it = tasks.iterator();
674
675      futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue));
676      --ntasks;
677      int active = 1;
678
679      while (true) {
680        Future<T> f = futureQueue.poll();
681        if (f == null) {
682          if (ntasks > 0) {
683            --ntasks;
684            futures.add(submitAndAddQueueListener(executorService, it.next(), futureQueue));
685            ++active;
686          } else if (active == 0) {
687            break;
688          } else if (timed) {
689            f = futureQueue.poll(timeoutNanos, TimeUnit.NANOSECONDS);
690            if (f == null) {
691              throw new TimeoutException();
692            }
693            long now = System.nanoTime();
694            timeoutNanos -= now - lastTime;
695            lastTime = now;
696          } else {
697            f = futureQueue.take();
698          }
699        }
700        if (f != null) {
701          --active;
702          try {
703            return f.get();
704          } catch (ExecutionException eex) {
705            ee = eex;
706          } catch (RuntimeException rex) {
707            ee = new ExecutionException(rex);
708          }
709        }
710      }
711
712      if (ee == null) {
713        ee = new ExecutionException(null);
714      }
715      throw ee;
716    } finally {
717      for (Future<T> f : futures) {
718        f.cancel(true);
719      }
720    }
721  }
722
723  /**
724   * Submits the task and adds a listener that adds the future to {@code queue} when it completes.
725   */
726  @GwtIncompatible // TODO
727  private static <T> ListenableFuture<T> submitAndAddQueueListener(
728      ListeningExecutorService executorService,
729      Callable<T> task,
730      final BlockingQueue<Future<T>> queue) {
731    final ListenableFuture<T> future = executorService.submit(task);
732    future.addListener(
733        new Runnable() {
734          @Override
735          public void run() {
736            queue.add(future);
737          }
738        },
739        directExecutor());
740    return future;
741  }
742
743  /**
744   * Returns a default thread factory used to create new threads.
745   *
746   * <p>On AppEngine, returns {@code ThreadManager.currentRequestThreadFactory()}. Otherwise,
747   * returns {@link Executors#defaultThreadFactory()}.
748   *
749   * @since 14.0
750   */
751  @Beta
752  @GwtIncompatible // concurrency
753  public static ThreadFactory platformThreadFactory() {
754    if (!isAppEngine()) {
755      return Executors.defaultThreadFactory();
756    }
757    try {
758      return (ThreadFactory)
759          Class.forName("com.google.appengine.api.ThreadManager")
760              .getMethod("currentRequestThreadFactory")
761              .invoke(null);
762    } catch (IllegalAccessException e) {
763      throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
764    } catch (ClassNotFoundException e) {
765      throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
766    } catch (NoSuchMethodException e) {
767      throw new RuntimeException("Couldn't invoke ThreadManager.currentRequestThreadFactory", e);
768    } catch (InvocationTargetException e) {
769      throw Throwables.propagate(e.getCause());
770    }
771  }
772
773  @GwtIncompatible // TODO
774  private static boolean isAppEngine() {
775    if (System.getProperty("com.google.appengine.runtime.environment") == null) {
776      return false;
777    }
778    try {
779      // If the current environment is null, we're not inside AppEngine.
780      return Class.forName("com.google.apphosting.api.ApiProxy")
781              .getMethod("getCurrentEnvironment")
782              .invoke(null)
783          != null;
784    } catch (ClassNotFoundException e) {
785      // If ApiProxy doesn't exist, we're not on AppEngine at all.
786      return false;
787    } catch (InvocationTargetException e) {
788      // If ApiProxy throws an exception, we're not in a proper AppEngine environment.
789      return false;
790    } catch (IllegalAccessException e) {
791      // If the method isn't accessible, we're not on a supported version of AppEngine;
792      return false;
793    } catch (NoSuchMethodException e) {
794      // If the method doesn't exist, we're not on a supported version of AppEngine;
795      return false;
796    }
797  }
798
799  /**
800   * Creates a thread using {@link #platformThreadFactory}, and sets its name to {@code name} unless
801   * changing the name is forbidden by the security manager.
802   */
803  @GwtIncompatible // concurrency
804  static Thread newThread(String name, Runnable runnable) {
805    checkNotNull(name);
806    checkNotNull(runnable);
807    Thread result = platformThreadFactory().newThread(runnable);
808    try {
809      result.setName(name);
810    } catch (SecurityException e) {
811      // OK if we can't set the name in this environment.
812    }
813    return result;
814  }
815
816  // TODO(lukes): provide overloads for ListeningExecutorService? ListeningScheduledExecutorService?
817  // TODO(lukes): provide overloads that take constant strings? Function<Runnable, String>s to
818  // calculate names?
819
820  /**
821   * Creates an {@link Executor} that renames the {@link Thread threads} that its tasks run in.
822   *
823   * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed
824   * right before each task is run. The renaming is best effort, if a {@link SecurityManager}
825   * prevents the renaming then it will be skipped but the tasks will still execute.
826   *
827   *
828   * @param executor The executor to decorate
829   * @param nameSupplier The source of names for each task
830   */
831  @GwtIncompatible // concurrency
832  static Executor renamingDecorator(final Executor executor, final Supplier<String> nameSupplier) {
833    checkNotNull(executor);
834    checkNotNull(nameSupplier);
835    if (isAppEngine()) {
836      // AppEngine doesn't support thread renaming, so don't even try
837      return executor;
838    }
839    return new Executor() {
840      @Override
841      public void execute(Runnable command) {
842        executor.execute(Callables.threadRenaming(command, nameSupplier));
843      }
844    };
845  }
846
847  /**
848   * Creates an {@link ExecutorService} that renames the {@link Thread threads} that its tasks run
849   * in.
850   *
851   * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed
852   * right before each task is run. The renaming is best effort, if a {@link SecurityManager}
853   * prevents the renaming then it will be skipped but the tasks will still execute.
854   *
855   *
856   * @param service The executor to decorate
857   * @param nameSupplier The source of names for each task
858   */
859  @GwtIncompatible // concurrency
860  static ExecutorService renamingDecorator(
861      final ExecutorService service, final Supplier<String> nameSupplier) {
862    checkNotNull(service);
863    checkNotNull(nameSupplier);
864    if (isAppEngine()) {
865      // AppEngine doesn't support thread renaming, so don't even try.
866      return service;
867    }
868    return new WrappingExecutorService(service) {
869      @Override
870      protected <T> Callable<T> wrapTask(Callable<T> callable) {
871        return Callables.threadRenaming(callable, nameSupplier);
872      }
873
874      @Override
875      protected Runnable wrapTask(Runnable command) {
876        return Callables.threadRenaming(command, nameSupplier);
877      }
878    };
879  }
880
881  /**
882   * Creates a {@link ScheduledExecutorService} that renames the {@link Thread threads} that its
883   * tasks run in.
884   *
885   * <p>The names are retrieved from the {@code nameSupplier} on the thread that is being renamed
886   * right before each task is run. The renaming is best effort, if a {@link SecurityManager}
887   * prevents the renaming then it will be skipped but the tasks will still execute.
888   *
889   *
890   * @param service The executor to decorate
891   * @param nameSupplier The source of names for each task
892   */
893  @GwtIncompatible // concurrency
894  static ScheduledExecutorService renamingDecorator(
895      final ScheduledExecutorService service, final Supplier<String> nameSupplier) {
896    checkNotNull(service);
897    checkNotNull(nameSupplier);
898    if (isAppEngine()) {
899      // AppEngine doesn't support thread renaming, so don't even try.
900      return service;
901    }
902    return new WrappingScheduledExecutorService(service) {
903      @Override
904      protected <T> Callable<T> wrapTask(Callable<T> callable) {
905        return Callables.threadRenaming(callable, nameSupplier);
906      }
907
908      @Override
909      protected Runnable wrapTask(Runnable command) {
910        return Callables.threadRenaming(command, nameSupplier);
911      }
912    };
913  }
914
915  /**
916   * Shuts down the given executor service gradually, first disabling new submissions and later, if
917   * necessary, cancelling remaining tasks.
918   *
919   * <p>The method takes the following steps:
920   * <ol>
921   * <li>calls {@link ExecutorService#shutdown()}, disabling acceptance of new submitted tasks.
922   * <li>awaits executor service termination for half of the specified timeout.
923   * <li>if the timeout expires, it calls {@link ExecutorService#shutdownNow()}, cancelling pending
924   * tasks and interrupting running tasks.
925   * <li>awaits executor service termination for the other half of the specified timeout.
926   * </ol>
927   *
928   * <p>If, at any step of the process, the calling thread is interrupted, the method calls
929   * {@link ExecutorService#shutdownNow()} and returns.
930   *
931   * @param service the {@code ExecutorService} to shut down
932   * @param timeout the maximum time to wait for the {@code ExecutorService} to terminate
933   * @param unit the time unit of the timeout argument
934   * @return {@code true} if the {@code ExecutorService} was terminated successfully, {@code false}
935   *     if the call timed out or was interrupted
936   * @since 17.0
937   */
938  @Beta
939  @CanIgnoreReturnValue
940  @GwtIncompatible // concurrency
941  public static boolean shutdownAndAwaitTermination(
942      ExecutorService service, long timeout, TimeUnit unit) {
943    long halfTimeoutNanos = unit.toNanos(timeout) / 2;
944    // Disable new tasks from being submitted
945    service.shutdown();
946    try {
947      // Wait for half the duration of the timeout for existing tasks to terminate
948      if (!service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS)) {
949        // Cancel currently executing tasks
950        service.shutdownNow();
951        // Wait the other half of the timeout for tasks to respond to being cancelled
952        service.awaitTermination(halfTimeoutNanos, TimeUnit.NANOSECONDS);
953      }
954    } catch (InterruptedException ie) {
955      // Preserve interrupt status
956      Thread.currentThread().interrupt();
957      // (Re-)Cancel if current thread also interrupted
958      service.shutdownNow();
959    }
960    return service.isTerminated();
961  }
962
963  /**
964   * Returns an Executor that will propagate {@link RejectedExecutionException} from the delegate
965   * executor to the given {@code future}.
966   *
967   * <p>Note, the returned executor can only be used once.
968   */
969  static Executor rejectionPropagatingExecutor(
970      final Executor delegate, final AbstractFuture<?> future) {
971    checkNotNull(delegate);
972    checkNotNull(future);
973    if (delegate == directExecutor()) {
974      // directExecutor() cannot throw RejectedExecutionException
975      return delegate;
976    }
977    return new Executor() {
978      volatile boolean thrownFromDelegate = true;
979
980      @Override
981      public void execute(final Runnable command) {
982        try {
983          delegate.execute(
984              new Runnable() {
985                @Override
986                public void run() {
987                  thrownFromDelegate = false;
988                  command.run();
989                }
990              });
991        } catch (RejectedExecutionException e) {
992          if (thrownFromDelegate) {
993            // wrap exception?
994            future.setException(e);
995          }
996          // otherwise it must have been thrown from a transitive call and the delegate runnable
997          // should have handled it.
998        }
999      }
1000    };
1001  }
1002}