Package org.apache.solr.common.util

## Class ExecutorUtil

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

    public class ExecutorUtil
    extends Object

### Nested Class Summary

Nested Classes
`static interface` `ExecutorUtil.InheritableThreadLocalProvider` Any class which wants to carry forward the threadlocal values to the threads run by threadpools must implement this interface and the implementation should be registered here
`static class` `ExecutorUtil.MDCAwareThreadPoolExecutor`

### Constructor Summary

Constructors
`ExecutorUtil()`

### Method Summary

All Methods Static Methods Concrete Methods
`static void` `addThreadLocalProvider​(ExecutorUtil.InheritableThreadLocalProvider provider)`
`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.
`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.
`static boolean` `isShutdown​(ExecutorService pool)`
`static boolean` `isSolrServerThread()`
`static boolean` `isTerminated​(ExecutorService pool)`
`static ExecutorService` `newMDCAwareCachedThreadPool​(int maxThreads, int queueCapacity, ThreadFactory threadFactory)` Create a new pool of threads.
`static ExecutorService` `newMDCAwareCachedThreadPool​(String name)` Create a cached thread pool using a named thread factory
`static ExecutorService` `newMDCAwareCachedThreadPool​(ThreadFactory threadFactory)` Create a new pool of threads, with no limit for the number of threads.
`static ExecutorService` `newMDCAwareFixedThreadPool​(int nThreads, int queueCapacity, ThreadFactory threadFactory, Runnable beforeExecute)`
`static ExecutorService` `newMDCAwareFixedThreadPool​(int nThreads, ThreadFactory threadFactory)` See `Executors.newFixedThreadPool(int, ThreadFactory)`
`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.
`static ExecutorService` `newMDCAwareSingleThreadExecutor​(ThreadFactory threadFactory)` See `Executors.newSingleThreadExecutor(ThreadFactory)`.
`static void` `resetThreadLocalProviders()` Resets everything added via `addThreadLocalProvider(InheritableThreadLocalProvider)`.
`static void` `setServerThreadFlag​(Boolean flag)`
`static void` `shutdownAndAwaitTermination​(ExecutorService pool)` Shutdown the `ExecutorService` and wait for 60 seconds for the threads to complete.
`static void` `shutdownAndAwaitTerminationForever​(ExecutorService pool)` Shutdown the `ExecutorService` and wait forever for the threads to complete.
`static void` `shutdownNowAndAwaitTermination​(ExecutorService pool)`
`static <T> Collection<T>` `submitAllAndAwaitAggregatingExceptions​(ExecutorService service, List<? extends Callable<T>> tasks)` Takes an executor and a list of Callables and executes them returning the results as a list.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### ExecutorUtil

    public ExecutorUtil()

### Method Detail

#### 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 shutdown 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 timebound when shutting down. However, sometimes `ExecutorService`s are used to submit a list of tasks and awaiting termination is akin to waiting on the list of `Future`s 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 shutdown 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)

See `Executors.newFixedThreadPool(int, ThreadFactory)`

#### newMDCAwareFixedThreadPool

    public static ExecutorService newMDCAwareFixedThreadPool​(int nThreads,
                                                             int queueCapacity,
                                                             ThreadFactory threadFactory,
                                                             Runnable beforeExecute)

#### 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