private long waitFor(double permitsToAwait,long t){
  long begin=t;
  try {
    while (permitsToAwait > 0) {
      double currentRate=this.rate;
      double nanosPerPermit=1000000000.0 / currentRate;
      long done=t + ((long)(permitsToAwait * nanosPerPermit));
      long now=System.nanoTime();
      if (done > now) {
        rateChanged.awaitNanos(done - now);
        now=System.nanoTime();
      }
      double permitsElapsed=((now - t) / 1000000000.0) * currentRate;
      permitsToAwait-=permitsElapsed;
      t=now;
    }
  }
 catch (  InterruptedException e) {
  }
  return t - begin;
}

private long waitFor ( double permitsToAwait , long t ) { long begin = t ; try { while ( permitsToAwait > 0 ) { double currentRate = this . rate ; double nanosPerPermit = 1000000000.0 / currentRate ; long done = t + ( ( long ) ( permitsToAwait * nanosPerPermit ) ) ; long now = System . nanoTime ( ) ; if ( done > now ) { rateChanged . awaitNanos ( done - now ) ; now = System . nanoTime ( ) ; } double permitsElapsed = ( ( now - t ) / 1000000000.0 ) * currentRate ; permitsToAwait -= permitsElapsed ; t = now ; } } catch ( InterruptedException e ) { } return t - begin ; }
must hold rate lock
