zoukankan      html  css  js  c++  java
  • RateLimiter之-tryAcquire(int permits, long timeout, TimeUnit unit)

    源码:

    public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
        long timeoutMicros = max(unit.toMicros(timeout), 0);
        checkPermits(permits);
        long microsToWait;
        synchronized (mutex()) {
          long nowMicros = stopwatch.readMicros();
          if (!canAcquire(nowMicros, timeoutMicros)) {
            return false;
          } else {
            microsToWait = reserveAndGetWaitLength(permits, nowMicros);
          }
        }
        stopwatch.sleepMicrosUninterruptibly(microsToWait);
        return true;
      }
    

      意思是,尝试能否在timeout时间内获取permits个许可,第三个参数是timeout的时间单位,是一个enum,例如:

    /**
         * Time unit representing sixty minutes
         */
        HOURS {
            public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
            public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
            public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
            public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
            public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
            public long toHours(long d)   { return d; }
            public long toDays(long d)    { return d/(C6/C5); }
            public long convert(long d, TimeUnit u) { return u.toHours(d); }
            int excessNanos(long d, long m) { return 0; }
        },
    

      

  • 相关阅读:
    Windows 系统变量大全
    linux编程
    CSS 对齐操作
    php 和 表单 简单交互
    HTML <input> placeholder 属性
    HTML <label> 标签
    Chap-4 Section 4.4 C++相关问题
    Chap-4 Section 4.3 COMMON块
    Chap-4 Section 4.2.4 指令修正方式
    Chap-4 Section 4.2.3 符号解析
  • 原文地址:https://www.cnblogs.com/lzh1043060917/p/13712737.html
Copyright © 2011-2022 走看看