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 Modifier and Type Class Description 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 herestatic class
ExecutorUtil.MDCAwareThreadPoolExecutor
-
Constructor Summary
Constructors Constructor Description ExecutorUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addThreadLocalProvider(ExecutorUtil.InheritableThreadLocalProvider provider)
static void
awaitTermination(ExecutorService pool)
Await the termination of anExecutorService
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 anExecutorService
until all threads are complete, or until we are interrupted, at which point theExecutorService
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)
static ExecutorService
newMDCAwareCachedThreadPool(String name)
Create a cached thread pool using a named thread factorystatic ExecutorService
newMDCAwareCachedThreadPool(ThreadFactory threadFactory)
static ExecutorService
newMDCAwareFixedThreadPool(int nThreads, ThreadFactory threadFactory)
static ExecutorService
newMDCAwareSingleLazyThreadExecutor(ThreadFactory threadFactory, long keepAliveTime, TimeUnit unit)
Similar tonewMDCAwareSingleThreadExecutor(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)
static void
resetThreadLocalProviders()
Resets everything added viaaddThreadLocalProvider(InheritableThreadLocalProvider)
.static void
setServerThreadFlag(Boolean flag)
static void
shutdownAndAwaitTermination(ExecutorService pool)
Shutdown theExecutorService
and wait for 60 seconds for the threads to complete.static void
shutdownAndAwaitTerminationForever(ExecutorService pool)
Shutdown theExecutorService
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.
-
-
-
Method Detail
-
resetThreadLocalProviders
public static void resetThreadLocalProviders()
Resets everything added viaaddThreadLocalProvider(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 theExecutorService
and wait for 60 seconds for the threads to complete. More detail on the waiting can be found inawaitTermination(ExecutorService)
.- Parameters:
pool
- The ExecutorService to shutdown and wait on
-
shutdownAndAwaitTerminationForever
public static void shutdownAndAwaitTerminationForever(ExecutorService pool)
Shutdown theExecutorService
and wait forever for the threads to complete. More detail on the waiting can be found inawaitTerminationForever(ExecutorService)
.This should likely not be used in
close()
methods, as we want to timebound when shutting down. However, sometimesExecutorService
s are used to submit a list of tasks and awaiting termination is akin to waiting on the list ofFuture
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 anExecutorService
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 anExecutorService
until all threads are complete, or until we are interrupted, at which point theExecutorService
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)
SeeExecutors.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 tonewMDCAwareSingleThreadExecutor(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)
-
newMDCAwareCachedThreadPool
public static ExecutorService newMDCAwareCachedThreadPool(int maxThreads, int queueCapacity, ThreadFactory threadFactory)
-
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
- executortasks
- the list of callables to be executed- Returns:
- results list
- Throws:
IOException
- in case any exceptions happened
-
-