Package com.zfabrik.work
Interface IThreadPool
-
- All Superinterfaces:
java.lang.AutoCloseable
public interface IThreadPool extends java.lang.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. NestedexecuteAs(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
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
close()
Terminate this thread pool (not waiting for tasks to complete) and unregister it with the work managerjava.util.List<java.util.concurrent.Future<?>>
execute(boolean block, java.lang.Runnable... runnables)
short hand method.java.util.List<java.util.concurrent.Future<?>>
execute(boolean block, java.util.Collection<? extends java.lang.Runnable> runnables)
Execute the passed on tasks concurrently within the bounds of the pools configured maximal concurrency.void
executeAs(java.lang.Runnable runnable, boolean maxconcurrency)
Incorporate the current thread logically into the pool, if it is not one of the pool's threads and execute the runnable.<T> T
executeAs(java.util.concurrent.Callable<T> runnable, boolean maxconcurrency)
Like above, but excepting aCallable
and thereby providing for an exception flow and a return value<T,E extends java.lang.Exception>
TgetAs(ThrowingSupplier<T,E> supplier, boolean observeMaxConcurrency)
Like above, but excepting aThrowingSupplier
and thereby providing for an exception flow and a return valueint
getMaxAchievedConcurrency()
get the max achieved concurrencyint
getMaxConcurrency()
sets the max concurrency for this thread pool.boolean
isPoolThread(java.lang.Thread t)
checks whether a given thread is of this pool<E extends java.lang.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 Detail
-
execute
java.util.List<java.util.concurrent.Future<?>> execute(boolean block, java.util.Collection<? extends java.lang.Runnable> runnables)
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
java.util.List<java.util.concurrent.Future<?>> execute(boolean block, java.lang.Runnable... runnables)
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
boolean isPoolThread(java.lang.Thread t)
checks whether a given thread is of this pool
-
executeAs
void executeAs(java.lang.Runnable runnable, boolean maxconcurrency)
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
<T> T executeAs(java.util.concurrent.Callable<T> runnable, boolean maxconcurrency) throws java.lang.Exception
Like above, but excepting aCallable
and thereby providing for an exception flow and a return value- Throws:
java.lang.Exception
-
getAs
<T,E extends java.lang.Exception> T getAs(ThrowingSupplier<T,E> supplier, boolean observeMaxConcurrency) throws E extends java.lang.Exception
Like above, but excepting aThrowingSupplier
and thereby providing for an exception flow and a return value- Throws:
E extends java.lang.Exception
-
runAs
<E extends java.lang.Exception> void runAs(ThrowingRunnable<E> runnable, boolean maxconcurrency) throws E extends java.lang.Exception
Like above, but excepting aThrowingRunnable
and thereby providing for a user defined exception.- Throws:
E extends java.lang.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 interfacejava.lang.AutoCloseable
-
-