Class ThreadUtil


  • public final class ThreadUtil
    extends Object
    • Method Detail

      • shutdownGracefully

        public static void shutdownGracefully​(@NotNull
                                              ExecutorService service,
                                              @NotNull
                                              String identifier)
        Gracefully stops executor tasks: stops executor, then waits for some time, then tries to interrupt running tasks and waits for termination of the executor.
        Parameters:
        service - executor to shut down
        identifier - executor identifier to use in log messages
      • shutdownGracefully

        public static void shutdownGracefully​(@NotNull
                                              ExecutorService service,
                                              @NotNull
                                              String identifier,
                                              long shutdownAwaitTimeout,
                                              long shutdownNowAwaitTimeout)
        Gracefully stops executor tasks: stops executor, then waits for some time, then tries to interrupt running tasks and waits for termination of the executor.
        Parameters:
        service - executor to shut down
        identifier - executor identifier to use in log messages
        shutdownAwaitTimeout - timeout in milliseconds to wait for the service shutdown
        shutdownNowAwaitTimeout - timeout in milliseconds to wait for the service shutdownNow
      • shutdownNowAndWait

        public static void shutdownNowAndWait​(@NotNull
                                              ExecutorService service,
                                              @NotNull
                                              String identifier)
        Interrupts executor tasks and waits for executor to finish.
        Parameters:
        service - executor to shut down
        identifier - executor identifier to use in log messages
      • waitForTasks

        @NotNull
        public static Map<Future,​Throwable> waitForTasks​(@NotNull
                                                               Collection<Future> futures)
        Waits for futures to complete, returns all errors occurred during the waiting.
        Parameters:
        futures - futures
        Returns:
        map from future to exception, if error occurred during the waiting.
      • sleep

        public static void sleep​(long millis)
        Sleeps for the specified time.

        Does like the Thread.sleep(long) does but doesn't throw exceptions.

        Parameters:
        millis - sleeping time in milliseconds.
      • awaitLatch

        public static void awaitLatch​(CountDownLatch latch)