Java TimeUnit Calculate sleepFor(long period, TimeUnit timeUnit)

Here you can find the source of sleepFor(long period, TimeUnit timeUnit)

Description

This method is a convenience method for sleep that "swallows" InterruptedException and has TimeUnit parameter in addition to time period so it makes it very convenient.

License

Open Source License

Parameter

Parameter Description
period time period to sleep
timeUnit TimeUnit that specifies in which units the time period is measured

Declaration

public static void sleepFor(long period, TimeUnit timeUnit) 

Method Source Code

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

import java.util.concurrent.TimeUnit;

public class Main {
    /**// www  .  j a  v a  2s  .  com
     * This method is a convenience method for sleep that "swallows" {@link InterruptedException}  and
     * has {@link TimeUnit} parameter in addition to time period so it makes it very convenient. So with
     * this method there is no need to convert the time into milliseconds. Just simply write
     * <br><br>
     * <p>{@code sleepFor(10, TimeUnit.SECONDS);}</p>
     * <br> No Exception catching needed
     * @param period time period to sleep
     * @param timeUnit {@link TimeUnit} that specifies in which units the time period is measured
     */
    public static void sleepFor(long period, TimeUnit timeUnit) {
        try {
            timeUnit.sleep(period);
        } catch (InterruptedException ie) {
        }
    }
}

Related

  1. sleep(long duration, TimeUnit timeUnit)
  2. sleep(long pTime, TimeUnit pTimeUnit)
  3. sleep(TimeUnit timeUnit, long duration)
  4. sleep(TimeUnit unit, long length)
  5. sleepAndCachedInterruptedException(long sleepFor, TimeUnit unit)
  6. timeDiff(Calendar end, Calendar begin, TimeUnit timeUnit)
  7. timeDiff(Date date1, Date date2, TimeUnit minutes)
  8. timeSince(final long startNanos, final TimeUnit destUnit)
  9. waitForEnd(Process process, long timeout, TimeUnit unit)