/**
   * Wait until futures are complete or the supplied timeout is reached.
   *
   * @param timeout Maximum time to wait for futures to complete.
   * @param unit Unit of time for the timeout.
   * @param futures Futures to wait for.
   * @return True if all futures complete in time.
   */
  public boolean awaitAll(long timeout, TimeUnit unit, Future<?>... futures) {
    boolean complete;

    try {
      // 将timeout 转换为微秒
      long nanos = unit.toNanos(timeout);
      // 获取系统当前时间的微秒
      long time = System.nanoTime();

      for (Future<?> f : futures) {
        if (nanos < 0) return false;
        // 此处f的示例为Command
        f.get(nanos, TimeUnit.NANOSECONDS);
        long now = System.nanoTime();
        nanos -= now - time;
        time = now;
      }

      complete = true;
    } catch (TimeoutException e) {
      complete = false;
    } catch (Exception e) {
      throw new RedisCommandInterruptedException(e);
    }

    return complete;
  }
Example #2
0
 /**
  * Set the default timeout for {@link RedisConnection connections} created by this client. The
  * timeout applies to connection attempts and non-blocking commands.
  *
  * @param timeout Default connection timeout.
  * @param unit Unit of time for the timeout.
  */
 public void setDefaultTimeout(long timeout, TimeUnit unit) {
   this.timeout = timeout;
   this.unit = unit;
   bootstrap.setOption("connectTimeoutMillis", unit.toMillis(timeout));
 }