Class ExecutorUtil


  • public class ExecutorUtil
    extends Object
    • Constructor Detail

      • ExecutorUtil

        public ExecutorUtil()
    • Method Detail

      • 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 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 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
      • newMDCAwareCachedThreadPool

        public static ExecutorService newMDCAwareCachedThreadPool​(String name)
        Create a cached thread pool using a named thread factory
      • 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 - executor
        tasks - the list of callables to be executed
        Returns:
        results list
        Throws:
        IOException - in case any exceptions happened