Java Code Examples for java.util.concurrent.TimeUnit#toMicros()

The following examples show how to use java.util.concurrent.TimeUnit#toMicros() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * Acquires the given number of permits from this {@code RateLimiter} if it can be obtained
 * without exceeding the specified {@code timeout}, or returns {@code false}
 * immediately (without waiting) if the permits would not have been granted
 * before the timeout expired.
 *
 * @param permits the number of permits to acquire
 * @param timeout the maximum time to wait for the permits
 * @param unit the time unit of the timeout argument
 * @return {@code true} if the permits were acquired, {@code false} otherwise
 */
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
    long timeoutMicros = unit.toMicros(timeout);
    checkPermits(permits);
    long microsToWait;
    synchronized (mutex) {
        long nowMicros = readSafeMicros();
        if (nextFreeTicketMicros > nowMicros + timeoutMicros) {
            return false;
        } else {
            microsToWait = reserveNextTicket(permits, nowMicros);
        }
    }
    ticker.sleepMicrosUninterruptibly(microsToWait);
    return true;
}
 
Example 2
Source Project: phoenix-tephra   File: DefaultFenceWait.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void await(long timeout, TimeUnit timeUnit)
  throws TransactionFailureException, InterruptedException, TimeoutException {
  Stopwatch stopwatch = new Stopwatch();
  stopwatch.start();
  long sleepTimeMicros = timeUnit.toMicros(timeout) / 10;
  // Have sleep time to be within 1 microsecond and 500 milliseconds
  sleepTimeMicros = Math.max(Math.min(sleepTimeMicros, 500 * 1000), 1);
  while (stopwatch.elapsedTime(timeUnit) < timeout) {
    txContext.start();
    try {
      txContext.finish();
      return;
    } catch (TransactionFailureException e) {
      LOG.error("Got exception waiting for fence. Sleeping for {} microseconds", sleepTimeMicros, e);
      txContext.abort();
      TimeUnit.MICROSECONDS.sleep(sleepTimeMicros);
    }
  }
  throw new TimeoutException("Timeout waiting for fence");
}
 
Example 3
@Override
public void registerFailedEvent(long eventLatency, TimeUnit unit) {
    double valueMillis = unit.toMicros(eventLatency) / 1000.0;

    failCountAdder.increment();
    failSumAdder.add((long) valueMillis);

    LocalData localData = current.localData.get();

    long stamp = localData.lock.readLock();
    try {
        localData.failSketch.update(valueMillis);
    } finally {
        localData.lock.unlockRead(stamp);
    }
}
 
Example 4
@Override
public void submit(String measurePointId, int elementCount, long elapsedTime, TimeUnit timeUnit) {
    if (measurePointId == null)
        throw new NullPointerException();
    if (measurePointId.isEmpty())
        throw new IllegalArgumentException();
    if (elapsedTime < 0)
        throw new IllegalArgumentException();
    if (elementCount < 0)
        throw new IllegalArgumentException();
    if (timeUnit == null)
        throw new NullPointerException();

    if ((dataFilter != null) && !dataFilter.accept(measurePointId, elementCount, elapsedTime, timeUnit)) {
        return;
    }
    StatSet ss = map.get(measurePointId);
    if (ss == null) {
        synchronized (mutex) {
            ss = map.get(measurePointId);
            if (ss == null) {
                ss = new StatSet(measurePointId);
                Map<String, StatSet> map2 = new HashMap<String, StatSet>(map);
                map2.put(measurePointId, ss);
                map = map2;
            }
        }
    }
    final long delta = timeUnit.toMicros(elapsedTime);
    synchronized (ss) {
        ss.elapsedTimeMicros += delta;
        ss.elementCount += elementCount;
        ss.count++;
    }
}
 
Example 5
Source Project: grpc-proxy   File: Recorder.java    License: Apache License 2.0 5 votes vote down vote up
public Recorder(long minLatency, long maxLatency, long goalLatency, TimeUnit latencyUnit) {
  this.goalLatency = latencyUnit.toMicros(goalLatency);
  this.count = new IntervalAdder();
  this.responseTime = new IntervalAdder();
  this.latency =
      new org.HdrHistogram.Recorder(
          latencyUnit.toMicros(minLatency), latencyUnit.toMicros(maxLatency), 1);
  this.histogram = latency.getIntervalHistogram(); // preload reporting histogram
}
 
Example 6
/**
 * 某个时间点之前的时间范围
 * @param end
 * @param unit
 * @return
 */
public static TimeRange before(long end, TimeUnit unit) {
    if (unit.toMicros(end) <= 0) {
        throw new ClientException("The end muse be positive.");
    }
    TimeRange timeRange = new TimeRange();
    timeRange.end = unit.toMicros(end);
    return timeRange;
}
 
Example 7
protected void setTimestamp(long timestamp, TimeUnit unit) {
    if (this.timeRange != null) {
        throw new ClientException("time range has been set");
    }
    if (timestamp < 0) {
        throw new ClientException("timestamp must be positive");
    }
    this.timestamp = unit.toMicros(timestamp);
}
 
Example 8
public Builder(long timestamp, TimeUnit unit) {
    if (timestamp < 0) {
        throw new ClientException("The timestamp must be positive.");
    }
    this.timestamp = unit.toMicros(timestamp);
    this.fieldList = new ArrayList<Column>();
}
 
Example 9
/**
 * 某个时间点之后的时间范围
 * @param begin
 * @param unit
 * @return
 */
public static TimeRange after(long begin, TimeUnit unit) {
    if (unit.toMicros(begin) <= 0) {
        throw new ClientException("The begin muse be positive.");
    }
    TimeRange timeRange = new TimeRange();
    timeRange.begin = unit.toMicros(begin);
    return timeRange;
}
 
Example 10
WarmingUp(SleepingTicker ticker, long warmupPeriod, TimeUnit timeUnit) {
    super(ticker);
    this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
}
 
Example 11
Source Project: neural   File: SmoothRateLimiter.java    License: MIT License 4 votes vote down vote up
SmoothWarmingUp(SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
    super(stopwatch);
    this.warmUpPeriodMicros = timeUnit.toMicros(warmupPeriod);
    this.coldFactor = coldFactor;
}
 
Example 12
Source Project: neural   File: SmoothRateLimiter.java    License: MIT License 4 votes vote down vote up
SmoothWarmingUp(SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
    super(stopwatch);
    this.warmUpPeriodMicros = timeUnit.toMicros(warmupPeriod);
    this.coldFactor = coldFactor;
}
 
Example 13
SmoothWarmingUp(SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
  super(stopwatch);
  this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
  this.coldFactor = coldFactor;
}
 
Example 14
SmoothWarmingUp(SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
  super(stopwatch);
  this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
  this.coldFactor = coldFactor;
}
 
Example 15
SmoothWarmingUp(SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
  super(stopwatch);
  this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
  this.coldFactor = coldFactor;
}
 
Example 16
SmoothWarmingUp(
    SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
  super(stopwatch);
  this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
  this.coldFactor = coldFactor;
}
 
Example 17
public TimestreamMeta setUpdateTime(long timestamp, TimeUnit unit) {
    this.updateTime = unit.toMicros(timestamp);
    return this;
}
 
Example 18
SmoothWarmingUp(
        SleepingStopwatch stopwatch, long warmupPeriod, TimeUnit timeUnit, double coldFactor) {
    super(stopwatch);
    this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
    this.coldFactor = coldFactor;
}
 
Example 19
Source Project: Jaffree   File: FFprobe.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Specify how long to analyze to probe the input (from 0 to I64_MAX) (default 0)
 * @param analyzeDuration duration
 * @param timeUnit time unit
 * @return this
 */
public FFprobe setAnalyzeDuration(Number analyzeDuration, TimeUnit timeUnit) {
    long micros = (long) (analyzeDuration.doubleValue() * timeUnit.toMicros(1));
    return setAnalyzeDuration(micros);
}
 
Example 20
Source Project: nomulus   File: BigqueryUtils.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Converts a time (in TimeUnits since the epoch) into a numeric string that BigQuery understands
 * as a timestamp: the decimal number of seconds since the epoch, precise up to microseconds.
 *
 * @see <a href="https://developers.google.com/bigquery/timestamp">Data Types</a>
 */
public static String toBigqueryTimestamp(long timestamp, TimeUnit unit) {
  long seconds = unit.toSeconds(timestamp);
  long fractionalSeconds = unit.toMicros(timestamp) % 1000000;
  return String.format("%d.%06d", seconds, fractionalSeconds);
}