Control ExecutorService to execute N tasks per second maximum

后端 未结 2 622
执念已碎
执念已碎 2021-02-01 11:01

How to control/limit the tasks that are submitted to a ExecutorService? I have SMSTask that sends SMS messages and I need to control the executor so that it can onl

相关标签:
2条回答
  • 2021-02-01 11:17

    Try RateLimiter from Guava. You must share one instance between all tasks running in the pool:

    final RateLimiter rateLimiter = RateLimiter.create(N)
    
    //and in your task:
    
    rateLimiter.tryAcquire();
    sendSms();
    

    tryAcquire(); will block for the amount of time precisely to preserve N frequency.

    See also:

    • RateLimiter - discovering Google Guava
    0 讨论(0)
  • 2021-02-01 11:25

    Assuming you are creating one SMS message per task you can use a ScheduleExecutorService.

    final Queue<Task> tasks = new ConcurrentLinkedQueue<Task>();
    int ratePerSecond = 10;
    final ExecutorService es = Executors.newCachedThreadPool();
    ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
    ses.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            final Task task = tasks.poll();
            if (task == null) return;
            es.submit(new Runnable() {
                @Override
                public void run() {
                    process(task);
                }
            });
        }
    }, 0, 1000/ratePerSecond, TimeUnit.MILLISECONDS);
    

    Add tasks to the queue and they will be processed at a rate of 10 per second.

    0 讨论(0)
提交回复
热议问题