java.lang.Object
org.refcodes.controlflow.ControlFlowUtility
public final class ControlFlowUtility extends Object
Utility class addressing control flow related issues.
-
Method Summary
Modifier and Type Method Description static ExecutorService
createCachedExecutorService(boolean isDaemon)
Returns a defaultExecutorService
retrieved from theExecutors
.static ScheduledExecutorService
createScheduledExecutorService(int aCorePoolSize, boolean isDaemon)
Returns a defaultExecutorService
retrieved from theExecutors
.static void
joinFutures(Collection<? extends Future<?>> aFutures)
Joins theFuture
instances contained in the givenCollection
.static void
joinFutures(Future<?>... aFutures)
Joins theFuture
instances contained in the givenCollection
.static void
joinThreads(Thread... aThreads)
Joins theThread
instances contained in the givenCollection
.static void
joinThreads(Collection<? extends Thread> aThreads)
Joins theThread
instances contained in the givenCollection
.static void
shutdownGracefully(ExecutorService aExecutorService)
Tries to orderly shutdown (ExecutorService.shutdown()
) all tasks of anExecutorService
, after a timeout ofIoRetryCount.NORM
milliseconds, shutdown is forced as ofExecutorService.shutdownNow()
. ------------------------------------------------------------------------- CAUTION: Do only invoke this methods onExecutorService
instances you manage exclusively yourself, do not apply this method onExecutorService
instances provided to you by your application server (e.g. an JEE server)static void
shutdownGracefully(ExecutorService aExecutorService, long aTimeoutInMs)
Tries to orderly shutdown (ExecutorService.shutdown()
) all tasks of anExecutorService
, after a given timeout in milliseconds, shutdown is forced as ofExecutorService.shutdownNow()
.static void
throwIllegalStateException(boolean isIllegalState)
Code redundancy preventing code snippet (in terms of aspect) throwing anIllegalStateException
in case an illegal statefalse
is being provided.static ExecutorService
toManagedExecutorService(ExecutorService aExecutorService)
Converts anExecutorService
to be aManagedExecutorService
with methodsExecutorService.shutdown()
andExecutorService.shutdownNow()
being disabled as them are to be managed by an application server.static Object
waitForFutures(Collection<? extends Future<?>> aFutures)
Joins theFuture
instances contained in the givenCollection
.static Object
waitForFutures(Future<?>... aFutures)
Joins theFuture
instances contained in the givenCollection
.
-
Method Details
-
throwIllegalStateException
Code redundancy preventing code snippet (in terms of aspect) throwing anIllegalStateException
in case an illegal statefalse
is being provided. This method can be called in the beginning of methods of instances which can be disposed (Disposable
) or destroyed (as oforg.refcodes.component.Destroyable
or decomposed (as oforg.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 accordingIllegalStateException
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
Joins theThread
instances contained in the givenCollection
.- Parameters:
aThreads
- TheCollection
containing theThread
instances to be joined.
-
joinThreads
Joins theThread
instances contained in the givenCollection
.- Parameters:
aThreads
- TheCollection
containing theThread
instances to be joined.
-
joinFutures
Joins theFuture
instances contained in the givenCollection
.- Parameters:
aFutures
- TheCollection
containing theFuture
instances to be joined.
-
joinFutures
Joins theFuture
instances contained in the givenCollection
.- Parameters:
aFutures
- TheCollection
containing theFuture
instances to be joined.
-
waitForFutures
Joins theFuture
instances contained in the givenCollection
. Returns the first result detected or throws the firstException
which occurred.- Parameters:
aFutures
- TheCollection
containing theFuture
instances to be joined.- Returns:
- the object
- Throws:
Exception
- the exception
-
waitForFutures
Joins theFuture
instances contained in the givenCollection
. Returns the first result detected or throws the firstException
which occurred.- Parameters:
aFutures
- TheCollection
containing theFuture
instances to be joined.- Returns:
- the object
- Throws:
Exception
- the exception
-
shutdownGracefully
Tries to orderly shutdown (ExecutorService.shutdown()
) all tasks of anExecutorService
, after a timeout ofIoRetryCount.NORM
milliseconds, shutdown is forced as ofExecutorService.shutdownNow()
. ------------------------------------------------------------------------- CAUTION: Do only invoke this methods onExecutorService
instances you manage exclusively yourself, do not apply this method onExecutorService
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
Tries to orderly shutdown (ExecutorService.shutdown()
) all tasks of anExecutorService
, after a given timeout in milliseconds, shutdown is forced as ofExecutorService.shutdownNow()
. No shutdown is forced in case a timeout of o is being provided. ------------------------------------------------------------------------- CAUTION: Do only invoke this methods onExecutorService
instances you manage exclusively yourself, do not apply this method onExecutorService
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
Converts anExecutorService
to be aManagedExecutorService
with methodsExecutorService.shutdown()
andExecutorService.shutdownNow()
being disabled as them are to be managed by an application server. Usually this method is being invoked with anExecutorService
originating from an application server. SeeManagedExecutorService
for more details.- Parameters:
aExecutorService
- TheExecutorService
to be transformed to aManagedExecutorService
.- Returns:
- The
ManagedExecutorService
from the providedExecutorService
.
-
createCachedExecutorService
Returns a defaultExecutorService
retrieved from theExecutors
.- Parameters:
isDaemon
- When true, then daemon threads are created by theExecutorService
returned.- Returns:
- An
ExecutorService
to be used.
-
createScheduledExecutorService
public static ScheduledExecutorService createScheduledExecutorService(int aCorePoolSize, boolean isDaemon)Returns a defaultExecutorService
retrieved from theExecutors
.- 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 theExecutorService
returned.- Returns:
- An
ScheduledExecutorService
to be used.
-