Class ControlFlowUtility

java.lang.Object
org.refcodes.controlflow.ControlFlowUtility

public final class ControlFlowUtility
extends Object
Utility class addressing control flow related issues.
  • Method Details

    • throwIllegalStateException

      public static void throwIllegalStateException​(boolean isIllegalState) throws IllegalStateException
      Code redundancy preventing code snippet (in terms of aspect) throwing an IllegalStateException in case an illegal state false is being provided. This method can be called in the beginning of methods of instances which can be disposed (Disposable) or destroyed (as of org.refcodes.component.Destroyable or decomposed (as of org.refcodes.component.Decomposeable). In such cases the "disposed", "destroyed", "decomposed" or similar flag is being passed.
      Parameters:
      isIllegalState - When true, then an illegal state is being flagged which causes the according IllegalStateException with a default message being thrown.
      Throws:
      IllegalStateException - Thrown in case an instance is in a state not allowing the invocation of the according method; for calling that method the instance is in an illegal state.
    • joinThreads

      public static void joinThreads​(Collection<? extends Thread> aThreads)
      Joins the Thread instances contained in the given Collection.
      Parameters:
      aThreads - The Collection containing the Thread instances to be joined.
    • joinThreads

      public static void joinThreads​(Thread... aThreads)
      Joins the Thread instances contained in the given Collection.
      Parameters:
      aThreads - The Collection containing the Thread instances to be joined.
    • joinFutures

      public static void joinFutures​(Collection<? extends Future<?>> aFutures)
      Joins the Future instances contained in the given Collection.
      Parameters:
      aFutures - The Collection containing the Future instances to be joined.
    • joinFutures

      public static void joinFutures​(Future<?>... aFutures)
      Joins the Future instances contained in the given Collection.
      Parameters:
      aFutures - The Collection containing the Future instances to be joined.
    • waitForFutures

      public static Object waitForFutures​(Collection<? extends Future<?>> aFutures) throws Exception
      Joins the Future instances contained in the given Collection. Returns the first result detected or throws the first Exception which occurred.
      Parameters:
      aFutures - The Collection containing the Future instances to be joined.
      Returns:
      the object
      Throws:
      Exception - the exception
    • waitForFutures

      public static Object waitForFutures​(Future<?>... aFutures) throws Exception
      Joins the Future instances contained in the given Collection. Returns the first result detected or throws the first Exception which occurred.
      Parameters:
      aFutures - The Collection containing the Future instances to be joined.
      Returns:
      the object
      Throws:
      Exception - the exception
    • shutdownGracefully

      public static void shutdownGracefully​(ExecutorService aExecutorService)
      Tries to orderly shutdown (ExecutorService.shutdown()) all tasks of an ExecutorService, after a timeout of IoRetryCount.NORM milliseconds, shutdown is forced as of ExecutorService.shutdownNow(). ------------------------------------------------------------------------- CAUTION: Do only invoke this methods on ExecutorService instances you manage exclusively yourself, do not apply this method on ExecutorService instances provided to you by your application server (e.g. an JEE server). -------------------------------------------------------------------------
      Parameters:
      aExecutorService - The service which's threads are to be shut down soon.
    • shutdownGracefully

      public static void shutdownGracefully​(ExecutorService aExecutorService, long aTimeoutInMs)
      Tries to orderly shutdown (ExecutorService.shutdown()) all tasks of an ExecutorService, after a given timeout in milliseconds, shutdown is forced as of ExecutorService.shutdownNow(). No shutdown is forced in case a timeout of o is being provided. ------------------------------------------------------------------------- CAUTION: Do only invoke this methods on ExecutorService instances you manage exclusively yourself, do not apply this method on ExecutorService instances provided to you by your application server (e.g. an JEE server). -------------------------------------------------------------------------
      Parameters:
      aExecutorService - The service which's threads are to be shut down after the given timeout.
      aTimeoutInMs - The timeout after which to force shutdown eventually, in case 0 is being provided, then no shutdown is being forced (all threads are executed till them terminate normally).
    • toManagedExecutorService

      public static ExecutorService toManagedExecutorService​(ExecutorService aExecutorService)
      Converts an ExecutorService to be a ManagedExecutorService with methods ExecutorService.shutdown() and ExecutorService.shutdownNow() being disabled as them are to be managed by an application server. Usually this method is being invoked with an ExecutorService originating from an application server. See ManagedExecutorService for more details.
      Parameters:
      aExecutorService - The ExecutorService to be transformed to a ManagedExecutorService.
      Returns:
      The ManagedExecutorService from the provided ExecutorService.
    • createCachedExecutorService

      public static ExecutorService createCachedExecutorService​(boolean isDaemon)
      Returns a default ExecutorService retrieved from the Executors.
      Parameters:
      isDaemon - When true, then daemon threads are created by the ExecutorService returned.
      Returns:
      An ExecutorService to be used.
    • createScheduledExecutorService

      public static ScheduledExecutorService createScheduledExecutorService​(int aCorePoolSize, boolean isDaemon)
      Returns a default ExecutorService retrieved from the Executors.
      Parameters:
      aCorePoolSize - The number of threads to keep in the pool, even if they are idle.
      isDaemon - When true, then daemon threads are created by the ExecutorService returned.
      Returns:
      An ScheduledExecutorService to be used.