Java TimeUnit Calculate sleep(long duration, TimeUnit timeUnit)

Here you can find the source of sleep(long duration, TimeUnit timeUnit)

Description

A convenience version of Thread.sleep that uses TimeUnits instead of raw milliseconds and swallows interrupted exceptions

License

Open Source License

Parameter

Parameter Description
duration a parameter
timeUnit a parameter

Declaration

public static void sleep(long duration, TimeUnit timeUnit) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**/*from   w ww . j av a 2  s  .c om*/
     * A convenience version of Thread.sleep that uses TimeUnits instead of raw
     * milliseconds and swallows interrupted exceptions
     *
     * @param duration
     * @param timeUnit
     */
    public static void sleep(long duration, TimeUnit timeUnit) {
        try {
            Thread.sleep(toMillis(duration, timeUnit));
        } catch (InterruptedException ex) {
            System.out.println("sleep interrupted: " + ex.getMessage());
        }
    }

    public static long toMillis(long duration, TimeUnit timeUnit) {
        return TimeUnit.MILLISECONDS.convert(duration, timeUnit);
    }
}

Related

  1. resetWithClockStep(long clockStep, TimeUnit clockStepUnit)
  2. run(final Runnable runnable, long timeout, TimeUnit unit)
  3. shortName(TimeUnit unit)
  4. sleep(int duration, TimeUnit unit)
  5. sleep(long duration, TimeUnit timeUnit)
  6. sleep(long pTime, TimeUnit pTimeUnit)
  7. sleep(TimeUnit timeUnit, long duration)
  8. sleep(TimeUnit unit, long length)
  9. sleepAndCachedInterruptedException(long sleepFor, TimeUnit unit)