Java Executor with throttling/throughput control

前端 未结 6 1125
半阙折子戏
半阙折子戏 2020-12-13 02:37

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 --

6条回答
  •  自闭症患者
    2020-12-13 02:47

    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

提交回复
热议问题