Package com.zfabrik.work
Interface IThreadPool
- All Superinterfaces:
AutoCloseable
thread pool abstraction provided by the work manager. Threadpools are intimately related to work units in that the threads of one pool
define outer boundaries for work units. Nested
executeAs(Runnable, boolean)
executions on the same pool
will share one work unit, while switching from one
pool to another will lead to independent work units.
Threads not owned by this pool may be joined into this pool (and hence the workunit mechanism and the max concurrency config) via the executeAs method.-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Terminate this thread pool (not waiting for tasks to complete) and unregister it with the work managershort hand method.execute
(boolean block, Collection<? extends Runnable> runnables) Execute the passed on tasks concurrently within the bounds of the pools configured maximal concurrency.void
Incorporate the current thread logically into the pool, if it is not one of the pool's threads and execute the runnable.<T> T
Like above, but excepting aCallable
and thereby providing for an exception flow and a return value<T,
E extends Exception>
TgetAs
(ThrowingSupplier<T, E> supplier, boolean observeMaxConcurrency) Like above, but excepting aThrowingSupplier
and thereby providing for an exception flow and a return valueint
get the max achieved concurrencyint
sets the max concurrency for this thread pool.boolean
checks whether a given thread is of this pool<E extends Exception>
voidrunAs
(ThrowingRunnable<E> runnable, boolean maxconcurrency) Like above, but excepting aThrowingRunnable
and thereby providing for a user defined exception.void
setMaxConcurrency
(int maxconcurrency) sets the max concurrency for this thread pool.
-
Method Details
-
execute
Execute the passed on tasks concurrently within the bounds of the pools configured maximal concurrency.In case a non-blocking execution is required, all tasks will be put into the threadpool's task queue. In extreme cases this may imply that no task will be started before the current thread returns to the pool.
In particular, if the calling thread is from the same pool, it is crucial to not have the calling thread wait for the completion of the tasks, as those may never get started as all the pool's threads are busy. If a number of tasks need to be completed and the current thread must not return to the pool before completion, this method should be called indicating a blocking execution- Parameters:
runnables
- an array of runnables to be executed.block
- iftrue
the current thread will not return until all runnables have executed. Iffalse
, the current thread will return immediately.- Returns:
Future
s representing the work in progress or already done, if block is passed as true- Throws:
WorkException
- if there occurred an uncaught and non-severe exception during execution of a runnable. Any runnables that has not been started yet will not be started at all.
-
execute
short hand method.- Parameters:
runnables
-block
- iftrue
the current thread will not return until all runnables have executed. Iffalse
, the current thread will not return if it is from the same thread pool and will return otherwise.- Returns:
Future
s representing the work in progress or already done, if block is passed as true
-
setMaxConcurrency
void setMaxConcurrency(int maxconcurrency) sets the max concurrency for this thread pool. This number of threads is guaranteed to be made available if needed. Any more active jobs will be queued -
getMaxConcurrency
int getMaxConcurrency()sets the max concurrency for this thread pool. This number of threads is guaranteed to be made available if needed. Any more active jobs will be queued -
getMaxAchievedConcurrency
int getMaxAchievedConcurrency()get the max achieved concurrency -
isPoolThread
checks whether a given thread is of this pool -
executeAs
Incorporate the current thread logically into the pool, if it is not one of the pool's threads and execute the runnable. While this may lead to an effective increase of overall concurrency, some frameworks leave no choice between risking deadlocks when switching threads or not being able to attach a suitable thread context when not switching threads (e.g. session invalidation in Jetty)... If maxconcurrency is set to true, the calling thread may need to wait in order to not exceed our configured max concurrency -
executeAs
Like above, but excepting aCallable
and thereby providing for an exception flow and a return value- Throws:
Exception
-
getAs
<T,E extends Exception> T getAs(ThrowingSupplier<T, E> supplier, boolean observeMaxConcurrency) throws ELike above, but excepting aThrowingSupplier
and thereby providing for an exception flow and a return value- Throws:
E extends Exception
-
runAs
Like above, but excepting aThrowingRunnable
and thereby providing for a user defined exception.- Throws:
E extends Exception
-
close
void close()Terminate this thread pool (not waiting for tasks to complete) and unregister it with the work manager- Specified by:
close
in interfaceAutoCloseable
-