Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * 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();
        }
    }
}