Enum TimeUnit

java.lang.Object
java.lang.Enum<TimeUnit>
java.util.concurrent.TimeUnit
All Implemented Interfaces:
Comparable<TimeUnit>

public enum TimeUnit extends Enum<TimeUnit>
A unit of time, for timeouts such as Future.get(long, TimeUnit). Conversions saturate: a value too large for the target unit becomes Long.MAX_VALUE or Long.MIN_VALUE rather than overflowing.
  • Enum Constant Details

    • NANOSECONDS

      public static final TimeUnit NANOSECONDS
      Billionths of a second.
    • MICROSECONDS

      public static final TimeUnit MICROSECONDS
      Millionths of a second.
    • MILLISECONDS

      public static final TimeUnit MILLISECONDS
      Thousandths of a second.
    • SECONDS

      public static final TimeUnit SECONDS
      Seconds.
    • MINUTES

      public static final TimeUnit MINUTES
      Minutes.
    • HOURS

      public static final TimeUnit HOURS
      Hours.
    • DAYS

      public static final TimeUnit DAYS
      Days.
  • Method Details

    • values

      public static TimeUnit[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static TimeUnit valueOf(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum type has no constant with the specified name
      NullPointerException - if the argument is null
    • convert

      public long convert(long sourceDuration, TimeUnit sourceUnit)
      sourceDuration in sourceUnit, converted to this unit.
      Parameters:
      sourceDuration - the duration
      sourceUnit - its unit
      Returns:
      the duration in this unit
    • toNanos

      public long toNanos(long d)
      d of this unit in nanoseconds.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • toMicros

      public long toMicros(long d)
      d of this unit in microseconds.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • toMillis

      public long toMillis(long d)
      d of this unit in milliseconds.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • toSeconds

      public long toSeconds(long d)
      d of this unit in seconds.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • toMinutes

      public long toMinutes(long d)
      d of this unit in minutes.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • toHours

      public long toHours(long d)
      d of this unit in hours.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • toDays

      public long toDays(long d)
      d of this unit in days.
      Parameters:
      d - the duration
      Returns:
      the converted duration
    • timedWait

      public void timedWait(Object obj, long timeout) throws InterruptedException
      Waits on obj for timeout of this unit, as obj.wait does.
      Parameters:
      obj - the monitor, which the caller must hold
      timeout - how long to wait
      Throws:
      InterruptedException - when the thread is interrupted
    • timedJoin

      public void timedJoin(Thread thread, long timeout) throws InterruptedException
      Waits up to timeout of this unit for thread to end, as thread.join does.
      Parameters:
      thread - the thread to wait for
      timeout - how long to wait
      Throws:
      InterruptedException - when the waiting thread is interrupted
    • sleep

      public void sleep(long timeout) throws InterruptedException
      Sleeps for timeout of this unit, as Thread.sleep does.
      Parameters:
      timeout - how long to sleep
      Throws:
      InterruptedException - when the thread is interrupted