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

The following examples show how to use java.util.concurrent.TimeUnit#toMicros() . 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 check out the related API usage on the sidebar.
Example 1
Source File: RateLimiter.java    From aliyun-tsdb-java-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * 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 File: DefaultFenceWait.java    From phoenix-tephra with 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
Source File: DataSketchesOpStatsLogger.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@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
Source File: TimeRange.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * 某个时间点之后的时间范围
 * @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 5
Source File: Recorder.java    From grpc-proxy with 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
Source File: TimeRange.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * 某个时间点之前的时间范围
 * @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
Source File: Point.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
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 8
Source File: LoggingStatisticCollector.java    From copper-engine with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: DataQuery.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
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 10
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" 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 11
Source File: SmoothRateLimiter.java    From light-task-scheduler with Apache License 2.0 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 File: TimestreamMeta.java    From aliyun-tablestore-java-sdk with Apache License 2.0 4 votes vote down vote up
public TimestreamMeta setUpdateTime(long timestamp, TimeUnit unit) {
    this.updateTime = unit.toMicros(timestamp);
    return this;
}
 
Example 13
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" 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 14
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" 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 15
Source File: SmoothRateLimiter.java    From codebuff with BSD 2-Clause "Simplified" 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 16
Source File: SmoothRateLimiter.java    From neural with 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 17
Source File: SmoothRateLimiter.java    From neural with 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 18
Source File: RateLimiter.java    From aliyun-tsdb-java-sdk with Apache License 2.0 4 votes vote down vote up
WarmingUp(SleepingTicker ticker, long warmupPeriod, TimeUnit timeUnit) {
    super(ticker);
    this.warmupPeriodMicros = timeUnit.toMicros(warmupPeriod);
}
 
Example 19
Source File: BigqueryUtils.java    From nomulus with 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);
}
 
Example 20
Source File: FFprobe.java    From Jaffree with 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);
}