Java TimeUnit Usage sleepNanos(long nanoDuration)

Here you can find the source of sleepNanos(long nanoDuration)

Description

sleep Nanos

License

LGPL

Declaration

public static void sleepNanos(long nanoDuration) 

Method Source Code

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

import java.util.concurrent.TimeUnit;

public class Main {
    private static final long SLEEP_PRECISION = TimeUnit.MILLISECONDS.toNanos(4);
    private static final long SPIN_YIELD_PRECISION = TimeUnit.MILLISECONDS.toNanos(2);

    public static void sleepNanos(long nanoDuration) {
        final long end = System.nanoTime() + nanoDuration;
        long timeLeft = nanoDuration;
        do {//ww w  .java2 s  .  c  om
            try {
                if (timeLeft > SLEEP_PRECISION) {
                    Thread.sleep(1);
                } else if (timeLeft > SPIN_YIELD_PRECISION) {
                    Thread.sleep(0);
                }
            } catch (Exception e) {
            }
            timeLeft = end - System.nanoTime();
        } while (timeLeft > 0);
    }
}

Related

  1. sleep(long millis)
  2. sleep(long milliseconds)
  3. sleepForSeconds(int seconds)
  4. sleepMilliseconds(long milliseconds)
  5. sleepNanos(long nanoDuration)
  6. sleepQuietly(int seconds)
  7. sleepSeconds(int secs)
  8. string2Millis(String duration)
  9. string2Millis(String duration)