/** * Attempt to get the specified amount of tokens within the specified timeout. If the tokens cannot be retrieved in the * specified timeout, the call will return false immediately, otherwise, the call will block until the tokens are available. * * @return true if the tokens are granted. * @throws InterruptedException */ public boolean getTokens(long tokens, long timeout, TimeUnit timeoutUnit) throws InterruptedException { long timeoutMillis = timeoutUnit.toMillis(timeout); long wait; synchronized (this) { wait = tryReserveTokens(tokens, timeoutMillis); } if (wait < 0) { return false; } if (wait == 0) { return true; } Thread.sleep(wait); return true; }
/** * Attempt to get the specified amount of tokens within the specified timeout. If the tokens cannot be retrieved in the * specified timeout, the call will return false immediately, otherwise, the call will block until the tokens are available. * * @return true if the tokens are granted. * @throws InterruptedException */ public boolean getTokens(long tokens, long timeout, TimeUnit timeoutUnit) throws InterruptedException { long timeoutMillis = timeoutUnit.toMillis(timeout); long wait; synchronized (this) { wait = tryReserveTokens(tokens, timeoutMillis); } if (wait < 0) { return false; } if (wait == 0) { return true; } Thread.sleep(wait); return true; }