Class ExecutorUtil

java.lang.Object
org.apache.solr.common.util.ExecutorUtil

public class ExecutorUtil extends Object
  • Constructor Details

    • ExecutorUtil

      public ExecutorUtil()
  • Method Details

    • resetThreadLocalProviders

      public static void resetThreadLocalProviders()
      Resets everything added via addThreadLocalProvider(InheritableThreadLocalProvider). Useful to call at the beginning of tests.
    • addThreadLocalProvider

      public static void addThreadLocalProvider(ExecutorUtil.InheritableThreadLocalProvider provider)
    • isShutdown

      public static boolean isShutdown(ExecutorService pool)
    • isTerminated

      public static boolean isTerminated(ExecutorService pool)
    • shutdownAndAwaitTermination

      public static void shutdownAndAwaitTermination(ExecutorService pool)
      Shutdown the ExecutorService and wait for 60 seconds for the threads to complete. More detail on the waiting can be found in awaitTermination(ExecutorService).
      Parameters:
      pool - The ExecutorService to shut down and wait on
    • shutdownAndAwaitTerminationForever

      public static void shutdownAndAwaitTerminationForever(ExecutorService pool)
      Shutdown the ExecutorService and wait forever for the threads to complete. More detail on the waiting can be found in awaitTerminationForever(ExecutorService).

      This should likely not be used in close() methods, as we want to time bound when shutting down. However, sometimes ExecutorServices are used to submit a list of tasks and awaiting termination is akin to waiting on the list of Futures to complete. In that case, this method should be used as there is no inherent time bound to waiting on those tasks to complete.

      Parameters:
      pool - The ExecutorService to shut down and wait on
    • shutdownNowAndAwaitTermination

      public static void shutdownNowAndAwaitTermination(ExecutorService pool)
    • awaitTermination

      public static void awaitTermination(ExecutorService pool)
      Await the termination of an ExecutorService for a default of 60 seconds, then force shutdown the remaining threads and wait another 60 seconds.
      Parameters:
      pool - the ExecutorService to wait on
    • awaitTerminationForever

      public static void awaitTerminationForever(ExecutorService pool)
      Await the termination of an ExecutorService until all threads are complete, or until we are interrupted, at which point the ExecutorService will be interrupted as well.
      Parameters:
      pool - the ExecutorService to wait on
    • newMDCAwareFixedThreadPool

      public static ExecutorService newMDCAwareFixedThreadPool(int nThreads, ThreadFactory threadFactory)
    • newMDCAwareSingleThreadExecutor

      public static ExecutorService newMDCAwareSingleThreadExecutor(ThreadFactory threadFactory)
      See Executors.newSingleThreadExecutor(ThreadFactory). Note the thread is always active, even if no tasks are submitted to the executor.
    • newMDCAwareSingleLazyThreadExecutor

      public static ExecutorService newMDCAwareSingleLazyThreadExecutor(ThreadFactory threadFactory, long keepAliveTime, TimeUnit unit)
      Similar to newMDCAwareSingleThreadExecutor(ThreadFactory), but the thread will not be kept active after the specified time if no task is submitted to the executor.
    • newMDCAwareCachedThreadPool

      public static ExecutorService newMDCAwareCachedThreadPool(String name)
      Create a cached thread pool using a named thread factory
    • newMDCAwareCachedThreadPool

      public static ExecutorService newMDCAwareCachedThreadPool(ThreadFactory threadFactory)
      Create a new pool of threads, with no limit for the number of threads. The pool has no task queue. Each submitted task is executed immediately, either by reusing an existing thread if one is available, or by starting a new thread. Unused threads will be closed after 60 seconds.
    • newMDCAwareCachedThreadPool

      public static ExecutorService newMDCAwareCachedThreadPool(int maxThreads, int queueCapacity, ThreadFactory threadFactory)
      Create a new pool of threads. Threads are created for new work if there is room to do so up to maxThreads. Beyond that, the queue is used up to queueCapacity. Beyond that, work is rejected with an exception. Unused threads will be closed after 60 seconds.
    • isSolrServerThread

      public static boolean isSolrServerThread()
    • setServerThreadFlag

      public static void setServerThreadFlag(Boolean flag)
    • submitAllAndAwaitAggregatingExceptions

      public static <T> Collection<T> submitAllAndAwaitAggregatingExceptions(ExecutorService service, List<? extends Callable<T>> tasks) throws IOException
      Takes an executor and a list of Callables and executes them returning the results as a list. The method waits for the return of every task even if one of them throws an exception. If any exception happens it will be thrown, wrapped into an IOException, and other following exceptions will be added as `addSuppressed` to the original exception
      Type Parameters:
      T - the response type
      Parameters:
      service - executor
      tasks - the list of callables to be executed
      Returns:
      results list
      Throws:
      IOException - in case any exceptions happened