具有限制/吞吐量控制的Java Executor [英] Java Executor with throttling/throughput control

查看:141
本文介绍了具有限制/吞吐量控制的Java Executor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个Java Executor,它允许我指定限制/吞吐量/起搏限制,例如,不超过100个任务可以在一秒钟内处理 - 如果更多的任务被提交,他们应该排队等待后来执行。这样做的主要目的是避免在遇到外部API或服务器时遇到限制。

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 -- if more tasks get submitted they should get queued and executed later. The main purpose of this is to avoid running into limits when hitting foreign APIs or servers.

我想知道是否有基础Java(我怀疑,因为我检查过)或其他可靠的地方(例如Apache Commons)提供这个,或者如果我必须自己编写。最好是轻量级的。我不介意自己写,但如果有一个标准版本,那么我至少要先看看它。

I'm wondering whether either base Java (which I doubt, because I checked) or somewhere else reliable (e.g. Apache Commons) provides this, or if I have to write my own. Preferably something lightweight. I don't mind writing it myself, but if there's a "standard" version out there somewhere I'd at least like to look at it first.

推荐答案

看看guavas RateLimiter


速率限制器。从概念上讲,速率限制器以
可配置的速率分配许可证。如果需要,每个acquire()都会阻塞,直到许可证
可用,然后接受它。一旦获得,许可证不必是
发布。速率限制器通常用于限制访问某些物理或逻辑资源
的速率。这与
Semaphore形成对比,后者限制并发访问的数量而不是
的速率(注意并发和速率密切相关,例如参见Little's Law)。

A rate limiter. Conceptually, a rate limiter distributes permits at a configurable rate. Each acquire() blocks if necessary until a permit is available, and then takes it. Once acquired, permits need not be released. Rate limiters are often used to restrict the rate at which some physical or logical resource is accessed. This is in contrast to Semaphore which restricts the number of concurrent accesses instead of the rate (note though that concurrency and rate are closely related, e.g. see Little's Law).

它的线程安全,但仍然 @Beta 。无论如何,可能值得一试。

Its threadsafe, but still @Beta. Might be worth a try anyway.

你必须将每次调用包装在执行者中。限制器。对于更干净的解决方案,您可以为 ExecutorService 创建某种包装。

You would have to wrap each call to the Executor with respect to the rate limiter. For a more clean solution you could create some kind of wrapper for the ExecutorService.

来自javadoc:

 final RateLimiter rateLimiter = RateLimiter.create(2.0); // rate is "2 permits per second"
  void submitTasks(List<Runnable> tasks, Executor executor) {
    for (Runnable task : tasks) {
      rateLimiter.acquire(); // may wait
      executor.execute(task);
    }
  }

这篇关于具有限制/吞吐量控制的Java Executor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆