@Test public void testRateLimiter() throws InterruptedException { final RateLimiter rateLimiter = RateLimiter.create(4.0); long start = System.nanoTime(); final AtomicLong okNum = new AtomicLong(0); final AtomicLong blockNum = new AtomicLong(0); for (int i = 0; i < 5; i++) { new Thread(new Runnable() { @Override public void run() { while (!stop) { if (rateLimiter.tryAcquire(1)) { okNum.incrementAndGet(); } else { blockNum.incrementAndGet(); } } } }).start(); } Thread.sleep(10 * 1000L); stop = true; System.out.println(System.nanoTime() - start); System.out.println("ok=" + okNum.get()); System.out.println("block=" + blockNum.get()); }
RateLimiter(SleepingStopwatch stopwatch) { Assert.notNull(stopwatch); this.stopwatch = stopwatch; }
RateLimiter { RateLimiter(SleepingStopwatch stopwatch) { Assert.notNull(stopwatch); this.stopwatch = stopwatch; } }
RateLimiter { RateLimiter(SleepingStopwatch stopwatch) { Assert.notNull(stopwatch); this.stopwatch = stopwatch; } RateLimiter(SleepingStopwatch stopwatch); }
RateLimiter { RateLimiter(SleepingStopwatch stopwatch) { Assert.notNull(stopwatch); this.stopwatch = stopwatch; } RateLimiter(SleepingStopwatch stopwatch); static RateLimiter create(double permitsPerSecond); static RateLimiter create(double permitsPerSecond, long warmupPeriod, TimeUnit unit); final void setRate(double permitsPerSecond); final double getRate(); double acquire(); double acquire(int permits); boolean tryAcquire(long timeout, TimeUnit unit); boolean tryAcquire(int permits); boolean tryAcquire(); boolean tryAcquire(int permits, long timeout, TimeUnit unit); @Override String toString(); }
RateLimiter { RateLimiter(SleepingStopwatch stopwatch) { Assert.notNull(stopwatch); this.stopwatch = stopwatch; } RateLimiter(SleepingStopwatch stopwatch); static RateLimiter create(double permitsPerSecond); static RateLimiter create(double permitsPerSecond, long warmupPeriod, TimeUnit unit); final void setRate(double permitsPerSecond); final double getRate(); double acquire(); double acquire(int permits); boolean tryAcquire(long timeout, TimeUnit unit); boolean tryAcquire(int permits); boolean tryAcquire(); boolean tryAcquire(int permits, long timeout, TimeUnit unit); @Override String toString();  }