I\'m looking for a Java Executor that allows me to specify throttling/throughput/pacing limitations, for example, no more than say 100 tasks can be processed in a second --
no more than say 100 tasks can be processed in a second -- if more tasks get submitted they should get queued and executed later
You need to look into Executors.newFixedThreadPool(int limit)
. This will allow you to limit the number of threads that can be executed simultaneously. If you submit more than one thread, they will be queued and executed later.
ExecutorService threadPool = Executors.newFixedThreadPool(100);
Future> result1 = threadPool.submit(runnable1);
Future> result2 = threadPool.submit(runnable2);
Futurte result3 = threadPool.submit(callable1);
...
Snippet above shows how you would work with an ExecutorService
that allows no more than 100 threads to be executed simultaneously.
Update:
After going over the comments, here is what I have come up with (kinda stupid). How about manually keeping a track of threads that are to be executed ? How about storing them first in an ArrayList
and then submitting them to the Executor
based on how many threads have already been executed in the last one second.
So, lets say 200 tasks have been submitted into our maintained ArrayList
, We can iterate and add 100 to the Executor
. When a second passes, we can add few more threads based on how many have completed in theExecutor
and so on