Java TimeUnit Calculate deepSleep(long sleepFor, TimeUnit unit)

Here you can find the source of deepSleep(long sleepFor, TimeUnit unit)

Description

sleep until timeout.

License

Apache License

Parameter

Parameter Description
nanos a parameter

Declaration

public final static void deepSleep(long sleepFor, TimeUnit unit) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**/*  w w w .j av a  2 s  .  c o m*/
     * sleep until timeout.
     * 
     * @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();
        }
    }
}

Related

  1. checkTime(long time, TimeUnit timeunit)
  2. checkTimeUnit(TimeUnit timeUnit)
  3. createEquidistantDates(Calendar reference, final int periods, final TimeUnit sampleUnit, final int sampleRate, Calendar calendar)
  4. dateDifference(Date date1, Date date2, TimeUnit timeUnit)
  5. dateIn(long distance, TimeUnit unit)
  6. delayQuietly(final long time, final TimeUnit unit)
  7. diff(Date earlier, Date later, TimeUnit timeUnit)
  8. differenceBetween(Date initDate, Date endDate, TimeUnit units)
  9. getBase(final TimeUnit unit)