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:org.apache.hadoop.hbase.procedure2.AbstractProcedureScheduler.java

@Override
public Procedure poll(long timeout, TimeUnit unit) {
    return poll(unit.toNanos(timeout));
}

From source file:org.apache.hadoop.hbase.util.BoundedPriorityBlockingQueue.java

public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
    if (e == null)
        throw new NullPointerException();
    long nanos = unit.toNanos(timeout);

    lock.lockInterruptibly();/*from w w w  .  j ava2s  . co m*/
    try {
        while (queue.remainingCapacity() == 0) {
            if (nanos <= 0)
                return false;
            nanos = notFull.awaitNanos(nanos);
        }
        this.queue.add(e);
        notEmpty.signal();
    } finally {
        lock.unlock();
    }
    return true;
}

From source file:org.apache.hadoop.hbase.util.BoundedPriorityBlockingQueue.java

public E poll(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    lock.lockInterruptibly();//from  ww  w. j a  v  a  2  s.  c o m
    E result = null;
    try {
        while (queue.size() == 0 && nanos > 0) {
            nanos = notEmpty.awaitNanos(nanos);
        }
        if (queue.size() > 0) {
            result = queue.poll();
        }
        notFull.signal();
    } finally {
        lock.unlock();
    }
    return result;
}

From source file:org.apache.hadoop.hive.metastore.tools.BenchmarkTool.java

private static void saveDataFile(String location, String name, DescriptiveStatistics data, TimeUnit scale) {
    long conv = scale.toNanos(1);
    Path dst = Paths.get(location, name);
    try (PrintStream output = new PrintStream(dst.toString())) {
        // Print all values one per line
        Arrays.stream(data.getValues()).forEach(d -> output.println(d / conv));
    } catch (FileNotFoundException e) {
        LOG.error("failed to write to {}", dst);
    }//from   www. j  av  a2  s  .co m
}

From source file:org.apache.nifi.controller.StandardProcessorNode.java

@Override
public void setRunDuration(final long duration, final TimeUnit timeUnit) {
    if (duration < 0) {
        throw new IllegalArgumentException("Run Duration must be non-negative value; cannot set to "
                + timeUnit.toSeconds(duration) + " seconds");
    }/*from w  w  w .j a v  a  2  s .co m*/

    this.runNanos = timeUnit.toNanos(duration);
}

From source file:org.codice.solr.factory.impl.SolrClientAdapter.java

@Override
public boolean isAvailable(long timeout, TimeUnit unit) throws InterruptedException {
    Validate.notNull(unit, "invalid null time unit");
    if (isAvailable()) { // quick check to avoid synchronization
        return true;
    }//from w  w w  .  j ava 2  s  .  com
    // letting now be recomputed by the timedWait() method allows us to better control testing
    long now = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis());
    final long end = now + unit.toNanos(timeout);

    synchronized (lock) {
        while (true) {
            if (state == State.CLOSED) {
                return false;
            }
            final boolean available = isAvailable();

            if (available) {
                return true;
            }
            final long timeRemaining = end - now;

            if (timeRemaining <= 0L) { // we timed out
                return false;
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Solr({}): waiting {} to become available", core,
                        DurationFormatUtils.formatDurationHMS(TimeUnit.NANOSECONDS.toMillis(timeRemaining)));
            }
            now = waiter.timedWait(lock, now, timeRemaining, TimeUnit.NANOSECONDS);
        }
    }
}

From source file:org.fusesource.meshkeeper.distribution.LaunchClient.java

public void waitForAvailableAgents(int agentCount, long timeout, TimeUnit unit)
        throws InterruptedException, TimeoutException {
    timeout = unit.toNanos(timeout);
    synchronized (this) {
        long start = System.nanoTime();
        while (timeout > 0 && agentProps.isEmpty()) {
            wait(timeout);//from  w w w . j  av a2  s .  c o m
            if (agentProps.size() >= agentCount) {
                return;
            }
            timeout -= System.nanoTime() - start;
        }

        if (agentProps.isEmpty()) {
            throw new TimeoutException();
        }
    }
}

From source file:org.jenkinsci.remoting.protocol.IOBufferMatcher.java

public boolean awaitStringContent(Matcher<String> matcher, long timeout, TimeUnit unit)
        throws InterruptedException {
    long giveUp = System.nanoTime() + unit.toNanos(timeout);
    state.lock();/*from   w ww  .  j  a va 2 s  .  co  m*/
    try {
        long remaining;
        while (0 < (remaining = giveUp - System.nanoTime())) {
            if (matcher.matches(asString())) {
                return true;
            }
            changed.await(remaining, TimeUnit.NANOSECONDS);
        }
        return false;
    } finally {
        state.unlock();
    }
}

From source file:org.jenkinsci.remoting.protocol.IOBufferMatcher.java

public boolean awaitByteContent(Matcher<byte[]> matcher, long timeout, TimeUnit unit)
        throws InterruptedException {
    long giveUp = System.nanoTime() + unit.toNanos(timeout);
    state.lock();/*w  ww  .j  a  va  2  s.  c  o m*/
    try {
        long remaining;
        while (0 < (remaining = giveUp - System.nanoTime())) {
            if (matcher.matches(asByteArray())) {
                return true;
            }
            changed.await(remaining, TimeUnit.NANOSECONDS);
        }
        return false;
    } finally {
        state.unlock();
    }
}

From source file:org.mule.util.monitor.ExpiryMonitor.java

/**
 * Adds an expirable object to monitor. If the Object is already being monitored
 * it will be reset and the millisecond timeout will be ignored
 *
 * @param value     the expiry value/*w  ww  . j  ava  2  s  . co m*/
 * @param timeUnit  The time unit of the Expiry value
 * @param expirable the objec that will expire
 */
public void addExpirable(long value, TimeUnit timeUnit, Expirable expirable) {
    if (isRegistered(expirable)) {
        resetExpirable(expirable);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Adding new expirable: " + expirable);
        }
        monitors.put(expirable, new ExpirableHolder(timeUnit.toNanos(value), expirable));
    }
}