Example usage for java.util.concurrent TimeUnit toNanos

List of usage examples for java.util.concurrent TimeUnit toNanos

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit toNanos.

Prototype

public long toNanos(long duration) 

Source Link

Document

Equivalent to #convert(long,TimeUnit) NANOSECONDS.convert(duration, this) .

Usage

From source file:LinkedTransferQueue.java

public E poll(long timeout, TimeUnit unit) throws InterruptedException {
    E e = xfer(null, false, TIMED, unit.toNanos(timeout));
    if (e != null || !Thread.interrupted())
        return e;
    throw new InterruptedException();
}

From source file:org.apache.solr.client.solrj.impl.CloudSolrClient.java

/**
 * Connect to a cluster.  If the cluster is not ready, retry connection up to a given timeout.
 * @param duration the timeout/*ww w . j a  va  2s  .co  m*/
 * @param timeUnit the units of the timeout
 * @throws TimeoutException if the cluster is not ready after the timeout
 * @throws InterruptedException if the wait is interrupted
 */
public void connect(long duration, TimeUnit timeUnit) throws TimeoutException, InterruptedException {
    log.info("Waiting for {} {} for cluster at {} to be ready", duration, timeUnit, zkHost);
    long timeout = System.nanoTime() + timeUnit.toNanos(duration);
    while (System.nanoTime() < timeout) {
        try {
            connect();
            log.info("Cluster at {} ready", zkHost);
            return;
        } catch (RuntimeException e) {
            // not ready yet, then...
        }
        TimeUnit.MILLISECONDS.sleep(250);
    }
    throw new TimeoutException("Timed out waiting for cluster");
}

From source file:org.apache.camel.processor.SamplingThrottler.java

public SamplingThrottler(Processor processor, long samplePeriod, TimeUnit units) {
    super(processor);

    if (samplePeriod <= 0) {
        throw new IllegalArgumentException("A positive value is required for the sampling period");
    }//  w  w w  . j  a va  2 s.c o  m
    if (units == null) {
        throw new IllegalArgumentException(
                "A invalid null value was supplied for the units of the sampling period");
    }
    this.samplePeriod = samplePeriod;
    this.units = units;
    this.periodInNanos = units.toNanos(samplePeriod);
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Creates a new {@code ContinuationsExecutor} with the given initial
 * parameters.//w w w  .j  a va2  s. c  o m
 * 
 * @param corePoolSize
 *            the number of threads to keep in the pool, even if they are
 *            idle, unless {@code allowCoreThreadTimeOut} is set
 * @param maximumPoolSize
 *            the maximum number of threads to allow in the pool
 * @param keepAliveTime
 *            when the number of threads is greater than the core, this is
 *            the maximum time that excess idle threads will wait for new
 *            tasks before terminating.
 * @param unit
 *            the time unit for the {@code keepAliveTime} argument
 * @param workQueue
 *            the queue to use for holding tasks before they are executed.
 *            This queue will hold only the {@code Runnable} tasks submitted
 *            by the {@code execute} method.
 * @param threadFactory
 *            the factory to use when the executor creates a new thread
 * @param handler
 *            the handler to use when execution is blocked because the
 *            thread bounds and queue capacities are reached
 * @throws IllegalArgumentException
 *             if one of the following holds:<br>
 *             {@code corePoolSize < 0}<br>
 *             {@code keepAliveTime < 0}<br>
 *             {@code maximumPoolSize <= 0}<br>
 *             {@code maximumPoolSize < corePoolSize}
 * @throws NullPointerException
 *             if {@code workQueue} or {@code threadFactory} or
 *             {@code handler} is null
 */
public ContinuationsExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
        BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
    if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0)
        throw new IllegalArgumentException();
    if (workQueue == null || threadFactory == null || handler == null)
        throw new NullPointerException();
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock mainLock = this.mainLock;
    mainLock.lock();//from w  w w .  j  ava  2  s .  c  o m
    try {
        for (;;) {
            if (runStateAtLeast(ctl.get(), TERMINATED))
                return true;
            if (nanos <= 0)
                return false;
            nanos = termination.awaitNanos(nanos);
        }
    } finally {
        mainLock.unlock();
    }
}

From source file:org.apache.cassandra.concurrent.ContinuationsExecutor.java

/**
 * Sets the time limit for which threads may remain idle before being
 * terminated. If there are more than the core number of threads currently
 * in the pool, after waiting this amount of time without processing a task,
 * excess threads will be terminated. This overrides any value set in the
 * constructor./*from w  w  w . j  ava 2  s.com*/
 * 
 * @param time
 *            the time to wait. A time value of zero will cause excess
 *            threads to terminate immediately after executing tasks.
 * @param unit
 *            the time unit of the {@code time} argument
 * @throws IllegalArgumentException
 *             if {@code time} less than zero or if {@code time} is zero and
 *             {@code allowsCoreThreadTimeOut}
 * @see #getKeepAliveTime
 */
public void setKeepAliveTime(long time, TimeUnit unit) {
    if (time < 0)
        throw new IllegalArgumentException();
    if (time == 0 && allowsCoreThreadTimeOut())
        throw new IllegalArgumentException("Core threads must have nonzero keep alive times");
    long keepAliveTime = unit.toNanos(time);
    long delta = keepAliveTime - this.keepAliveTime;
    this.keepAliveTime = keepAliveTime;
    if (delta < 0)
        interruptIdleWorkers();
}

From source file:org.apache.hadoop.hbase.client.RawAsyncTableImpl.java

@Override
public void setReadRpcTimeout(long timeout, TimeUnit unit) {
    this.readRpcTimeoutNs = unit.toNanos(timeout);
}

From source file:org.apache.hadoop.hbase.client.RawAsyncTableImpl.java

@Override
public void setWriteRpcTimeout(long timeout, TimeUnit unit) {
    this.writeRpcTimeoutNs = unit.toNanos(timeout);
}

From source file:org.apache.hadoop.hbase.client.RawAsyncTableImpl.java

@Override
public void setOperationTimeout(long timeout, TimeUnit unit) {
    this.operationTimeoutNs = unit.toNanos(timeout);
}

From source file:org.apache.hadoop.hbase.client.RawAsyncTableImpl.java

@Override
public void setScanTimeout(long timeout, TimeUnit unit) {
    this.scanTimeoutNs = unit.toNanos(timeout);
}