Class ThreadUtil


  • public final class ThreadUtil
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void awaitLatch​(java.util.concurrent.CountDownLatch latch)  
      static void shutdownGracefully​(java.util.concurrent.ExecutorService service, java.lang.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.
      static void shutdownGracefully​(java.util.concurrent.ExecutorService service, java.lang.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.
      static void shutdownNowAndWait​(java.util.concurrent.ExecutorService service, java.lang.String identifier)
      Interrupts executor tasks and waits for executor to finish.
      static void sleep​(long millis)
      Sleeps for the specified time.
      static java.util.Map<java.util.concurrent.Future,​java.lang.Throwable> waitForTasks​(java.util.Collection<java.util.concurrent.Future> futures)
      Waits for futures to complete, returns all errors occurred during the waiting.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • shutdownGracefully

        public static void shutdownGracefully​(@NotNull
                                              java.util.concurrent.ExecutorService service,
                                              @NotNull
                                              java.lang.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
                                              java.util.concurrent.ExecutorService service,
                                              @NotNull
                                              java.lang.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
                                              java.util.concurrent.ExecutorService service,
                                              @NotNull
                                              java.lang.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 java.util.Map<java.util.concurrent.Future,​java.lang.Throwable> waitForTasks​(@NotNull
                                                                                                        java.util.Collection<java.util.concurrent.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​(java.util.concurrent.CountDownLatch latch)