@Test public void testTryAcquire() throws Exception { int interval = 100; TimeUnit timeUnit = TimeUnit.MILLISECONDS; TimeIntervalLimiter limiter = Concurrents.timeIntervalLimiter(interval, timeUnit); assertThat(limiter.tryAcquire()).isTrue(); assertThat(limiter.tryAcquire()).isFalse(); timeUnit.sleep(interval); assertThat(limiter.tryAcquire()).isTrue(); }
public boolean tryAcquire() { long currentTime = System.currentTimeMillis(); long lastTime = lastTimeAtom.get(); return currentTime - lastTime >= windowSizeMillis && lastTimeAtom.compareAndSet(lastTime, currentTime); }
TimeIntervalLimiter { public boolean tryAcquire() { long currentTime = System.currentTimeMillis(); long lastTime = lastTimeAtom.get(); return currentTime - lastTime >= windowSizeMillis && lastTimeAtom.compareAndSet(lastTime, currentTime); } }
TimeIntervalLimiter { public boolean tryAcquire() { long currentTime = System.currentTimeMillis(); long lastTime = lastTimeAtom.get(); return currentTime - lastTime >= windowSizeMillis && lastTimeAtom.compareAndSet(lastTime, currentTime); } TimeIntervalLimiter(long interval, TimeUnit timeUnit); }
TimeIntervalLimiter { public boolean tryAcquire() { long currentTime = System.currentTimeMillis(); long lastTime = lastTimeAtom.get(); return currentTime - lastTime >= windowSizeMillis && lastTimeAtom.compareAndSet(lastTime, currentTime); } TimeIntervalLimiter(long interval, TimeUnit timeUnit); boolean tryAcquire(); }
TimeIntervalLimiter { public boolean tryAcquire() { long currentTime = System.currentTimeMillis(); long lastTime = lastTimeAtom.get(); return currentTime - lastTime >= windowSizeMillis && lastTimeAtom.compareAndSet(lastTime, currentTime); } TimeIntervalLimiter(long interval, TimeUnit timeUnit); boolean tryAcquire();  }