Package org.refcodes.controlflow
Class ControlFlowUtility
- java.lang.Object
-
- org.refcodes.controlflow.ControlFlowUtility
-
public final class ControlFlowUtility extends java.lang.Object
Utility class addressing control flow related issues.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.concurrent.ExecutorService
createCachedExecutorService(boolean isDaemon)
Returns a defaultExecutorService
retrieved from theExecutors
.static java.util.concurrent.ScheduledExecutorService
createScheduledExecutorService(int aCorePoolSize, boolean isDaemon)
Returns a defaultExecutorService
retrieved from theExecutors
.static void
joinFutures(java.util.Collection<? extends java.util.concurrent.Future<?>> aFutures)
Joins theFuture
instances contained in the givenCollection
.static void
joinFutures(java.util.concurrent.Future<?>... aFutures)
Joins theFuture
instances contained in the givenCollection
.static void
joinThreads(java.lang.Thread... aThreads)
Joins theThread
instances contained in the givenCollection
.static void
joinThreads(java.util.Collection<? extends java.lang.Thread> aThreads)
Joins theThread
instances contained in the givenCollection
.static void
shutdownGracefully(java.util.concurrent.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(java.util.concurrent.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 java.util.concurrent.ExecutorService
toManagedExecutorService(java.util.concurrent.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 java.lang.Object
waitForFutures(java.util.Collection<? extends java.util.concurrent.Future<?>> aFutures)
Joins theFuture
instances contained in the givenCollection
.static java.lang.Object
waitForFutures(java.util.concurrent.Future<?>... aFutures)
Joins theFuture
instances contained in the givenCollection
.
-
-
-
Method Detail
-
throwIllegalStateException
public static void throwIllegalStateException(boolean isIllegalState) throws java.lang.IllegalStateException
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:
java.lang.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(java.util.Collection<? extends java.lang.Thread> aThreads)
Joins theThread
instances contained in the givenCollection
.- Parameters:
aThreads
- TheCollection
containing theThread
instances to be joined.
-
joinThreads
public static void joinThreads(java.lang.Thread... aThreads)
Joins theThread
instances contained in the givenCollection
.- Parameters:
aThreads
- TheCollection
containing theThread
instances to be joined.
-
joinFutures
public static void joinFutures(java.util.Collection<? extends java.util.concurrent.Future<?>> aFutures)
Joins theFuture
instances contained in the givenCollection
.- Parameters:
aFutures
- TheCollection
containing theFuture
instances to be joined.
-
joinFutures
public static void joinFutures(java.util.concurrent.Future<?>... aFutures)
Joins theFuture
instances contained in the givenCollection
.- Parameters:
aFutures
- TheCollection
containing theFuture
instances to be joined.
-
waitForFutures
public static java.lang.Object waitForFutures(java.util.Collection<? extends java.util.concurrent.Future<?>> aFutures) throws java.lang.Exception
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:
java.lang.Exception
- the exception
-
waitForFutures
public static java.lang.Object waitForFutures(java.util.concurrent.Future<?>... aFutures) throws java.lang.Exception
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:
java.lang.Exception
- the exception
-
shutdownGracefully
public static void shutdownGracefully(java.util.concurrent.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). -------------------------------------------------------------------------- Parameters:
aExecutorService
- The service which's threads are to be shut down soon.
-
shutdownGracefully
public static void shutdownGracefully(java.util.concurrent.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()
. 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
public static java.util.concurrent.ExecutorService toManagedExecutorService(java.util.concurrent.ExecutorService aExecutorService)
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
public static java.util.concurrent.ExecutorService createCachedExecutorService(boolean isDaemon)
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 java.util.concurrent.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.
-
-