SimpleRateLimiter { public synchronized boolean canExecute() { long now = System.currentTimeMillis(); if ((now - lastUpdate.get()) > timeWindow) { lastUpdate.set(now); countDownLatch.set(operationPerWindow); } return countDownLatch.decrementAndGet() >= 0; } SimpleRateLimiter(int timeWindow, int operationPerWindow); synchronized boolean canExecute();  }
@Test public void shouldTestTheRateLimiter() throws InterruptedException { SimpleRateLimiter srl = new SimpleRateLimiter(1000, 1); boolean canExecute = srl.canExecute(); boolean cantExecute = srl.canExecute(); Thread.sleep(2000); boolean nowCanExecute = srl.canExecute(); assertTrue(canExecute); assertFalse(cantExecute); assertTrue(nowCanExecute); }