Class MoreExecutors

java.lang.Object
org.docx4j.com.google.common.util.concurrent.MoreExecutors

@GwtCompatible(emulated=true)
public final class MoreExecutors
extends java.lang.Object
Factory and utility methods for Executor, ExecutorService, and ThreadFactory.
Since:
3.0
Author:
Eric Fellheimer, Kyle Littlefield, Justin Mahoney
  • Method Summary

    Modifier and Type Method Description
    static java.util.concurrent.Executor directExecutor()
    Returns an Executor that runs each task in the thread that invokes execute, as in ThreadPoolExecutor.CallerRunsPolicy.
    static ListeningExecutorService newDirectExecutorService()
    Creates an executor service that runs each task in the thread that invokes execute/submit, as in ThreadPoolExecutor.CallerRunsPolicy This applies both to individually submitted tasks and to collections of tasks submitted via invokeAll or invokeAny.
    static java.util.concurrent.ThreadFactory platformThreadFactory()
    Returns a default thread factory used to create new threads.
    static boolean shutdownAndAwaitTermination​(java.util.concurrent.ExecutorService service, long timeout, java.util.concurrent.TimeUnit unit)
    Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • newDirectExecutorService

      @GwtIncompatible public static ListeningExecutorService newDirectExecutorService()
      Creates an executor service that runs each task in the thread that invokes execute/submit, as in ThreadPoolExecutor.CallerRunsPolicy This applies both to individually submitted tasks and to collections of tasks submitted via invokeAll or invokeAny. In the latter case, tasks will run serially on the calling thread. Tasks are run to completion before a Future is returned to the caller (unless the executor has been shutdown).

      Although all tasks are immediately executed in the thread that submitted the task, this ExecutorService imposes a small locking overhead on each task submission in order to implement shutdown and termination behavior.

      The implementation deviates from the ExecutorService specification with regards to the shutdownNow method. First, "best-effort" with regards to canceling running tasks is implemented as "no-effort". No interrupts or other attempts are made to stop threads executing tasks. Second, the returned list will always be empty, as any submitted task is considered to have started execution. This applies also to tasks given to invokeAll or invokeAny which are pending serial execution, even the subset of the tasks that have not yet started execution. It is unclear from the ExecutorService specification if these should be included, and it's much easier to implement the interpretation that they not be. Finally, a call to shutdown or shutdownNow may result in concurrent calls to invokeAll/invokeAny throwing RejectedExecutionException, although a subset of the tasks may already have been executed.

      Since:
      18.0 (present as MoreExecutors.sameThreadExecutor() since 10.0)
    • directExecutor

      public static java.util.concurrent.Executor directExecutor()
      Returns an Executor that runs each task in the thread that invokes execute, as in ThreadPoolExecutor.CallerRunsPolicy.

      This instance is equivalent to:

      
       final class DirectExecutor implements Executor {
         public void execute(Runnable r) {
           r.run();
         }
       }
       

      This should be preferred to newDirectExecutorService() because implementing the ExecutorService subinterface necessitates significant performance overhead.

      Since:
      18.0
    • platformThreadFactory

      @Beta @GwtIncompatible public static java.util.concurrent.ThreadFactory platformThreadFactory()
      Returns a default thread factory used to create new threads.

      On AppEngine, returns ThreadManager.currentRequestThreadFactory(). Otherwise, returns Executors.defaultThreadFactory().

      Since:
      14.0
    • shutdownAndAwaitTermination

      @Beta @CanIgnoreReturnValue @GwtIncompatible public static boolean shutdownAndAwaitTermination​(java.util.concurrent.ExecutorService service, long timeout, java.util.concurrent.TimeUnit unit)
      Shuts down the given executor service gradually, first disabling new submissions and later, if necessary, cancelling remaining tasks.

      The method takes the following steps:

      1. calls ExecutorService.shutdown(), disabling acceptance of new submitted tasks.
      2. awaits executor service termination for half of the specified timeout.
      3. if the timeout expires, it calls ExecutorService.shutdownNow(), cancelling pending tasks and interrupting running tasks.
      4. awaits executor service termination for the other half of the specified timeout.

      If, at any step of the process, the calling thread is interrupted, the method calls ExecutorService.shutdownNow() and returns.

      Parameters:
      service - the ExecutorService to shut down
      timeout - the maximum time to wait for the ExecutorService to terminate
      unit - the time unit of the timeout argument
      Returns:
      true if the ExecutorService was terminated successfully, false if the call timed out or was interrupted
      Since:
      17.0