List of usage examples for java.util.concurrent TimeUnit toNanos
public long toNanos(long duration)
From source file:Main.java
public static void main(String[] args) { TimeUnit tu = TimeUnit.DAYS; System.out.println(tu.toDays(1)); System.out.println(tu.toHours(1)); System.out.println(tu.toNanos(1)); }
From source file:Main.java
/** * Get the expected value of {@link System#nanoTime} after the given period has passed. Negative values * for {@code time} return a time from the past. Note that, since an overflow wrap-around may occur * at any time in the system's nanosecond clock, comparisons between the current time and this method's * result are non-trivial. For example, to test whether the result time {@code future} has passed: * {@code System.nanoTime() - future > 0}. (This checks that the current time is within a Long.MAX_VALUE range * <em>after</em> {@code future}, regardless of the absolute numeric values. We can infer that (most likely) * {@code future} is less than 292 years in the past, or (unlikely) future is more than 292 years in the future.) *//*w w w . j av a 2 s . c o m*/ public static long futureTimeNanos(long time, TimeUnit unit) { return System.nanoTime() + unit.toNanos(time); }
From source file:Main.java
public static final void sleep(long pTime, TimeUnit pTimeUnit) { final long lStart = System.nanoTime(); long lDeadlineInNanos = lStart + pTimeUnit.toNanos(pTime); boolean lSleepTimeBelowMillisecond = pTimeUnit.toMillis(pTime) == 0; long lNanoTime; while ((lNanoTime = System.nanoTime()) < lDeadlineInNanos) { try {// www .j av a 2s .c om if (lSleepTimeBelowMillisecond) { long lTimeToWaitInNanos = 3 * (lDeadlineInNanos - lNanoTime) / 4; if (lTimeToWaitInNanos > 0) Thread.sleep(0, (int) lTimeToWaitInNanos); } else { long lTimeToWaitInNanos = 3 * (lDeadlineInNanos - lNanoTime) / 4; if (lTimeToWaitInNanos > 0) { long lTimeToWaitInMillis = TimeUnit.NANOSECONDS.toMillis(lTimeToWaitInNanos); Thread.sleep(lTimeToWaitInMillis, (int) (lTimeToWaitInNanos % 1000000L)); } } } catch (InterruptedException e) { } } }
From source file:Main.java
/** * sleep until timeout./* w ww . ja v a2 s. c o m*/ * * @param nanos */ public final static void deepSleep(long sleepFor, TimeUnit unit) { if (sleepFor < 0) { throw new IllegalArgumentException("sleepFor can't be minus."); } long startTimeInNanos = System.nanoTime(); long leftNanos = unit.toNanos(sleepFor); boolean isInterrupted = false; while (leftNanos > 0) { try { TimeUnit.NANOSECONDS.sleep(leftNanos); leftNanos = 0; } catch (InterruptedException e) { isInterrupted = true; leftNanos -= (System.nanoTime() - startTimeInNanos); } } if (isInterrupted) { Thread.currentThread().interrupt(); } }
From source file:at.ac.univie.isc.asio.tool.Timeout.java
/** * @param timeout literal value of the timeout * @param unit the time unit of the literal value, e.g. {@link java.util.concurrent.TimeUnit#SECONDS} * @return timeout with a defined value or {@link #undefined()} if given value is less than zero. *//*from ww w . j a va2 s. c o m*/ @Nonnull public static Timeout from(final long timeout, @Nonnull final TimeUnit unit) { if (timeout < 0) { return UNDEFINED; } return new Timeout(unit.toNanos(timeout)); }
From source file:com.andrewkroh.cicso.rtp.AudioFileStreamer.java
/** * Returns the number of samples that represent the specified time period. * * @param outputFormat// www .j a va 2s . co m * format of the data, needed to obtain the sample rate * @param timePeriod * period of time * @param timeUnit * TimeUnit of the {@code timePeriod} * @return number of samples that represent the specified time period */ @VisibleForTesting public static int getNumberOfSamplesPerTimePeriod(AudioFormat outputFormat, long timePeriod, TimeUnit timeUnit) { double timePeriodSec = timeUnit.toNanos(timePeriod) / 1E9; double numberOfSamples = timePeriodSec * outputFormat.getSampleRate(); return (int) Math.ceil(numberOfSamples); }
From source file:com.streamsets.datacollector.util.SystemProcessImpl.java
/** * Java 1.7 does not have Process.waitFor(timeout) *///from w w w . ja v a2 s .com private static boolean waitFor(Process process, long timeout, TimeUnit unit) { long startTime = System.nanoTime(); long rem = unit.toNanos(timeout); do { try { process.exitValue(); return true; } catch (IllegalThreadStateException ex) { if (rem > 0) ThreadUtil.sleep(Math.min(TimeUnit.NANOSECONDS.toMillis(rem) + 1, 100)); } rem = unit.toNanos(timeout) - (System.nanoTime() - startTime); } while (rem > 0); return false; }
From source file:Main.java
/** * Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested * {@code numElements} elements are not available, it will wait for them up to the specified * timeout./*from w w w.j a va 2s . com*/ * * Taken from Google Guava 18.0 Queues * * @param q the blocking queue to be drained * @param buffer where to add the transferred elements * @param numElements the number of elements to be waited for * @param timeout how long to wait before giving up, in units of {@code unit} * @param unit a {@code TimeUnit} determining how to interpret the timeout parameter * @param <E> the type of the queue * @return the number of elements transferred * @throws InterruptedException if interrupted while waiting */ public static <E> int drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements, long timeout, TimeUnit unit) throws InterruptedException { buffer = Objects.requireNonNull(buffer); /* * This code performs one System.nanoTime() more than necessary, and in return, the time to * execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make * the timeout arbitrarily inaccurate, given a queue that is slow to drain). */ long deadline = System.nanoTime() + unit.toNanos(timeout); int added = 0; while (added < numElements) { // we could rely solely on #poll, but #drainTo might be more efficient when there are multiple // elements already available (e.g. LinkedBlockingQueue#drainTo locks only once) added += q.drainTo(buffer, numElements - added); if (added < numElements) { // not enough elements immediately available; will have to poll E e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS); if (e == null) { break; // we already waited enough, and there are no more elements in sight } buffer.add(e); added++; } } return added; }
From source file:metlos.executors.batch.BatchExecutor.java
protected static BatchRecord createNewBatchRecord(int nofElements, TimeUnit unit, long duration, long initialDelay) { BatchRecord batchRecord = new BatchRecord(); batchRecord.nofElements = nofElements; long now = now() + unit.toNanos(initialDelay); batchRecord.nextElementStartTime.set(now); batchRecord.finishTimeNanos = now + unit.toNanos(duration); return batchRecord; }
From source file:Main.java
public TimeSliceTask(final long timeToLive, final TimeUnit timeUnit) { this.timeToLive = System.nanoTime() + timeUnit.toNanos(timeToLive); this.duration = timeUnit.toMillis(timeToLive); }