Class ExecutorLimiter
- java.lang.Object
-
- org.threadly.concurrent.wrapper.limiter.ExecutorLimiter
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,SubmitterExecutor
- Direct Known Subclasses:
SubmitterSchedulerLimiter
public class ExecutorLimiter extends java.lang.Object implements SubmitterExecutor
This class is designed to limit how much parallel execution happens on a providedExecutor
. 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
.- Since:
- 4.6.0 (since 1.0.0 at org.threadly.concurrent.limiter)
-
-
Constructor Summary
Constructors Constructor Description ExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency)
Construct a new execution limiter that implements theExecutor
interface.ExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency, boolean limitFutureListenersExecution)
Construct a new execution limiter that implements theExecutor
interface.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(java.lang.Runnable task)
int
getMaxConcurrency()
Call to check what the maximum concurrency this limiter will allow.int
getUnsubmittedTaskCount()
Query how many tasks are being withheld from the parent scheduler.void
setMaxConcurrency(int maxConcurrency)
Updates the concurrency limit for this limiter.<T> ListenableFuture<T>
submit(java.lang.Runnable task, T result)
Submit a task to run as soon as possible.<T> ListenableFuture<T>
submit(java.util.concurrent.Callable<T> task)
Submit aCallable
to run as soon as possible.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.threadly.concurrent.SubmitterExecutor
submit
-
-
-
-
Constructor Detail
-
ExecutorLimiter
public ExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency)
Construct a new execution limiter that implements theExecutor
interface.- Parameters:
executor
-Executor
to submit task executions to.maxConcurrency
- maximum quantity of tasks to run in parallel
-
ExecutorLimiter
public ExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency, boolean limitFutureListenersExecution)
Construct a new execution limiter that implements theExecutor
interface.This constructor allows you to specify if listeners /
FutureCallback
's / functions inListenableFuture.map(java.util.function.Function)
orListenableFuture.flatMap(java.util.function.Function)
should be counted towards the concurrency limit. Specifyingfalse
will release the limit as soon as the original task completes. Specifyingtrue
will continue to enforce the limit until all listeners (without an executor) complete.- Parameters:
executor
-Executor
to submit task executions to.maxConcurrency
- maximum quantity of tasks to run in parallellimitFutureListenersExecution
-true
to include listener / mapped functions towards execution limit
-
-
Method Detail
-
execute
public void execute(java.lang.Runnable task)
- Specified by:
execute
in interfacejava.util.concurrent.Executor
-
submit
public <T> ListenableFuture<T> submit(java.lang.Runnable task, T result)
Description copied from interface:SubmitterExecutor
Submit a task to run as soon as possible. TheFuture.get()
method will return the provided result once the runnable has completed.- Specified by:
submit
in interfaceSubmitterExecutor
- Type Parameters:
T
- type of result for future- Parameters:
task
- runnable to be executedresult
- result to be returned from resulting future .get() when runnable completes- Returns:
- a future to know when the task has completed
-
submit
public <T> ListenableFuture<T> submit(java.util.concurrent.Callable<T> task)
Description copied from interface:SubmitterExecutor
Submit aCallable
to run as soon as possible. This is needed when a result needs to be consumed from the callable.- Specified by:
submit
in interfaceSubmitterExecutor
- Type Parameters:
T
- type of result returned from the future- Parameters:
task
- callable to be executed- Returns:
- a future to know when the task has completed and get the result of the callable
-
getMaxConcurrency
public int getMaxConcurrency()
Call to check what the maximum concurrency this limiter will allow.- Returns:
- maximum concurrent tasks to be run
-
setMaxConcurrency
public void setMaxConcurrency(int maxConcurrency)
Updates the concurrency limit for this limiter. If reducing the the limit, there will be no attempt or impact on tasks already limiting. Instead new tasks just wont be submitted to the parent pool until existing tasks complete and go below the new limit.- Parameters:
maxConcurrency
- maximum quantity of tasks to run in parallel- Since:
- 5.4
-
getUnsubmittedTaskCount
public int getUnsubmittedTaskCount()
Query how many tasks are being withheld from the parent scheduler. Returning the size of the queued tasks waiting for submission to the pool.- Returns:
- Quantity of tasks queued in this limiter
-
-