java.lang.Object
org.refcodes.controlflow.ControlFlowUtility
Utility class addressing control flow related issues.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ExecutorServicecreateCachedExecutorService(boolean isDaemon) Returns a defaultExecutorServiceretrieved from theExecutors.static ScheduledExecutorServicecreateScheduledExecutorService(int aCorePoolSize, boolean isDaemon) Returns a defaultExecutorServiceretrieved from theExecutors.static voidjoinFutures(Collection<? extends Future<?>> aFutures) Joins theFutureinstances contained in the givenCollection.static voidjoinFutures(Future<?>... aFutures) Joins theFutureinstances contained in the givenCollection.static voidjoinThreads(Thread... aThreads) Joins theThreadinstances contained in the givenCollection.static voidjoinThreads(Collection<? extends Thread> aThreads) Joins theThreadinstances contained in the givenCollection.static voidshutdownGracefully(ExecutorService aExecutorService) Tries to orderly shutdown (ExecutorService.shutdown()) all tasks of anExecutorService, after a timeout ofIoRetryCount.NORMmilliseconds, shutdown is forced as ofExecutorService.shutdownNow(). ------------------------------------------------------------------------- CAUTION: Do only invoke this methods onExecutorServiceinstances you manage exclusively yourself, do not apply this method onExecutorServiceinstances provided to you by your application server (e.g. an JEE server)static voidshutdownGracefully(ExecutorService aExecutorService, long aTimeoutMillis) Tries to orderly shutdown (ExecutorService.shutdown()) all tasks of anExecutorService, after a given timeout in milliseconds, shutdown is forced as ofExecutorService.shutdownNow().static voidthrowIllegalStateException(boolean isIllegalState) Code redundancy preventing code snippet (in terms of aspect) throwing anIllegalStateExceptionin case an illegal statefalseis being provided.static ExecutorServicetoManagedExecutorService(ExecutorService aExecutorService) Converts anExecutorServiceto be aManagedExecutorServicewith methodsExecutorService.shutdown()andExecutorService.shutdownNow()being disabled as them are to be managed by an application server.static ObjectwaitForFutures(Collection<? extends Future<?>> aFutures) Joins theFutureinstances contained in the givenCollection.static ObjectwaitForFutures(Future<?>... aFutures) Joins theFutureinstances contained in the givenCollection.
-
Method Details
-
throwIllegalStateException
Code redundancy preventing code snippet (in terms of aspect) throwing anIllegalStateExceptionin case an illegal statefalseis 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.Destroyableor 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 accordingIllegalStateExceptionwith 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 theThreadinstances contained in the givenCollection.- Parameters:
aThreads- TheCollectioncontaining theThreadinstances to be joined.
-
joinThreads
Joins theThreadinstances contained in the givenCollection.- Parameters:
aThreads- TheCollectioncontaining theThreadinstances to be joined.
-
joinFutures
Joins theFutureinstances contained in the givenCollection.- Parameters:
aFutures- TheCollectioncontaining theFutureinstances to be joined.
-
joinFutures
Joins theFutureinstances contained in the givenCollection.- Parameters:
aFutures- TheCollectioncontaining theFutureinstances to be joined.
-
waitForFutures
public static Object waitForFutures(Collection<? extends Future<?>> aFutures) throws ExecutionException, InterruptedException Joins theFutureinstances contained in the givenCollection. Returns the first result detected or throws the firstExceptionwhich occurred.- Parameters:
aFutures- TheCollectioncontaining theFutureinstances to be joined.- Returns:
- the object
- Throws:
ExecutionException- thrown when attempting to retrieve the result of a task that aborted by throwing an exception.InterruptedException- Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted
-
waitForFutures
Joins theFutureinstances contained in the givenCollection. Returns the first result detected or throws the firstExceptionwhich occurred.- Parameters:
aFutures- TheCollectioncontaining theFutureinstances 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.NORMmilliseconds, shutdown is forced as ofExecutorService.shutdownNow(). ------------------------------------------------------------------------- CAUTION: Do only invoke this methods onExecutorServiceinstances you manage exclusively yourself, do not apply this method onExecutorServiceinstances 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 onExecutorServiceinstances you manage exclusively yourself, do not apply this method onExecutorServiceinstances 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.aTimeoutMillis- 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 anExecutorServiceto be aManagedExecutorServicewith methodsExecutorService.shutdown()andExecutorService.shutdownNow()being disabled as them are to be managed by an application server. Usually this method is being invoked with anExecutorServiceoriginating from an application server. SeeManagedExecutorServicefor more details.- Parameters:
aExecutorService- TheExecutorServiceto be transformed to aManagedExecutorService.- Returns:
- The
ManagedExecutorServicefrom the providedExecutorService.
-
createCachedExecutorService
Returns a defaultExecutorServiceretrieved from theExecutors.- Parameters:
isDaemon- When true, then daemon threads are created by theExecutorServicereturned.- Returns:
- An
ExecutorServiceto be used.
-
createScheduledExecutorService
public static ScheduledExecutorService createScheduledExecutorService(int aCorePoolSize, boolean isDaemon) Returns a defaultExecutorServiceretrieved 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 theExecutorServicereturned.- Returns:
- An
ScheduledExecutorServiceto be used.
-