public class ExecutorLimiter extends java.lang.Object implements SubmitterExecutor
Executor
. This allows the user to have one thread pool for all their code, and if they
want certain sections to have less levels of parallelism (possibly because those those sections
would completely consume the global pool), they can wrap the executor in this class.
Thus providing you better control on the absolute thread count and how much parallelism can occur in different sections of the program.
This is an alternative from having to create multiple thread pools. By using this you also are able to accomplish more efficiently thread use than multiple thread pools would.
If limiting to a single thread, please see SingleThreadSchedulerSubPool
as a possible
alternative.
If you wish to have the tasks execute in an order other than FIFO please see
OrderedExecutorLimiter
.
Constructor and Description |
---|
ExecutorLimiter(java.util.concurrent.Executor executor,
int maxConcurrency)
Construct a new execution limiter that implements the
Executor interface. |
ExecutorLimiter(java.util.concurrent.Executor executor,
int maxConcurrency,
boolean limitFutureListenersExecution)
Construct a new execution limiter that implements the
Executor interface. |
Modifier and Type | Method and Description |
---|---|
void |
execute(java.lang.Runnable task) |
int |
getMaxConcurrency()
Call to check what the maximum concurrency this limiter will allow.
|
int |
getUnsubmittedTaskCount()
Returns how many tasks are currently being "limited" and thus are in queue to run from this
limiter.
|
void |
setMaxConcurrency(int maxConcurrency)
Updates the concurrency limit for this wrapper.
|
<T> ListenableFuture<T> |
submit(java.util.concurrent.Callable<T> task)
Submit a
Callable to run as soon as possible. |
<T> ListenableFuture<T> |
submit(java.lang.Runnable task,
T result)
Submit a task to run as soon as possible.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
submit
public ExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency)
Executor
interface.executor
- Executor
to submit task executions to.maxConcurrency
- maximum quantity of tasks to run in parallelpublic ExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency, boolean limitFutureListenersExecution)
Executor
interface.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
executor
- Executor
to submit task executions to.maxConcurrency
- maximum quantity of tasks to run in parallellimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitpublic void execute(java.lang.Runnable task)
execute
in interface java.util.concurrent.Executor
public <T> ListenableFuture<T> submit(java.lang.Runnable task, T result)
SubmitterExecutor
Future.get()
method will
return the provided result once the runnable has completed.submit
in interface SubmitterExecutor
T
- type of result for futuretask
- runnable to be executedresult
- result to be returned from resulting future .get() when runnable completespublic <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> task)
SubmitterExecutor
Callable
to run as soon as possible. This is needed when a result needs to
be consumed from the callable.submit
in interface SubmitterExecutor
T
- type of result returned from the futuretask
- callable to be executedpublic int getMaxConcurrency()
public void setMaxConcurrency(int maxConcurrency)
maxConcurrency
- maximum quantity of tasks to run in parallelpublic int getUnsubmittedTaskCount()