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 interfaceExecutorUtil.InheritableThreadLocalProviderAny class which wants to carry forward the thread local values to the threads run by thread pools must implement this interface and the implementation should be registered herestatic classExecutorUtil.MDCAwareThreadPoolExecutor
-
Constructor Summary
Constructors Constructor Description ExecutorUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddThreadLocalProvider(ExecutorUtil.InheritableThreadLocalProvider provider)static voidawaitTermination(ExecutorService pool)Await the termination of anExecutorServicefor a default of 60 seconds, then force shutdown the remaining threads and wait another 60 seconds.static voidawaitTerminationForever(ExecutorService pool)Await the termination of anExecutorServiceuntil all threads are complete, or until we are interrupted, at which point theExecutorServicewill be interrupted as well.static booleanisShutdown(ExecutorService pool)static booleanisSolrServerThread()static booleanisTerminated(ExecutorService pool)static ExecutorServicenewMDCAwareCachedThreadPool(int maxThreads, int queueCapacity, ThreadFactory threadFactory)Create a new pool of threads.static ExecutorServicenewMDCAwareCachedThreadPool(String name)Create a cached thread pool using a named thread factorystatic ExecutorServicenewMDCAwareCachedThreadPool(ThreadFactory threadFactory)Create a new pool of threads, with no limit for the number of threads.static ExecutorServicenewMDCAwareFixedThreadPool(int nThreads, ThreadFactory threadFactory)static ExecutorServicenewMDCAwareSingleLazyThreadExecutor(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 ExecutorServicenewMDCAwareSingleThreadExecutor(ThreadFactory threadFactory)static voidresetThreadLocalProviders()Resets everything added viaaddThreadLocalProvider(InheritableThreadLocalProvider).static voidsetServerThreadFlag(Boolean flag)static voidshutdownAndAwaitTermination(ExecutorService pool)Shutdown theExecutorServiceand wait for 60 seconds for the threads to complete.static voidshutdownAndAwaitTerminationForever(ExecutorService pool)Shutdown theExecutorServiceand wait forever for the threads to complete.static voidshutdownNowAndAwaitTermination(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 theExecutorServiceand wait for 60 seconds for the threads to complete. More detail on the waiting can be found inawaitTermination(ExecutorService).- Parameters:
pool- The ExecutorService to shut down and wait on
-
shutdownAndAwaitTerminationForever
public static void shutdownAndAwaitTerminationForever(ExecutorService pool)
Shutdown theExecutorServiceand 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 time bound when shutting down. However, sometimesExecutorServices are used to submit a list of tasks and awaiting termination is akin to waiting on the list ofFutures 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 anExecutorServicefor 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 anExecutorServiceuntil all threads are complete, or until we are interrupted, at which point theExecutorServicewill 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)
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 tomaxThreads. Beyond that, the queue is used up toqueueCapacity. 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- executortasks- the list of callables to be executed- Returns:
- results list
- Throws:
IOException- in case any exceptions happened
-
-