Java Executor with throttling/throughput control

前端 未结 6 1128
半阙折子戏
半阙折子戏 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 03:02

    The Java Executor doesn't offer such a limitation, only limitation by amount of threads, which is not what you are looking for.

    In general the Executor is the wrong place to limit such actions anyway, it should be at the moment where the Thread tries to call the outside server. You can do this for example by having a limiting Semaphore that threads wait on before they submit their requests.

    Calling Thread:

    public void run() {
      // ...
      requestLimiter.acquire();
      connection.send();
      // ...
     }
    

    While at the same time you schedule a (single) secondary thread to periodically (like every 60 seconds) releases acquired resources:

     public void run() {
      // ...
      requestLimiter.drainPermits();  // make sure not more than max are released by draining the Semaphore empty
      requestLimiter.release(MAX_NUM_REQUESTS);
      // ...
     }
    

提交回复
热议问题