ratelimiter


Guava

RateLimiter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
RateLimiter rateLimiter = RateLimiter.create(10);
for (int i = 0; i < 20; i++) {
int finalI = i;
new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
if (rateLimiter.tryAcquire()) {
cnt++;
System.out.println("thread: " + finalI + " cnt: " + cnt);
}
}
}
}).start();
}
Thread.sleep(1000 * 100 * 1000);

源码流程图:


文章作者: fightinggg
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 fightinggg !
  目录