java.util.concurrent
Enum TimeUnit

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.lang.Enum sample code for java.lang.Enum definition code for java.lang.Enum <TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit >
      extended by java.util.concurrent.TimeUnit
All Implemented Interfaces:
Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Comparable sample code for java.lang.Comparable definition code for java.lang.Comparable <TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit >

public enum TimeUnit
extends Enum sample code for java.lang.Enum definition code for java.lang.Enum <TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit >

A TimeUnit represents time durations at a given unit of granularity and provides utility methods to convert across units, and to perform timing and delay operations in these units. A TimeUnit does not maintain time information, but only helps organize and use time representations that may be maintained separately across various contexts.

A TimeUnit is mainly used to inform time-based methods how a given timing parameter should be interpreted. For example, the following code will timeout in 50 milliseconds if the lock sample code for java.util.concurrent.locks.Lock definition code for java.util.concurrent.locks.Lock is not available:

  Lock lock = ...;
  if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
 
while this code will timeout in 50 seconds:
  Lock lock = ...;
  if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
 
Note however, that there is no guarantee that a particular timeout implementation will be able to notice the passage of time at the same granularity as the given TimeUnit.

Since:
1.5

Enum Constant Summary
MICROSECONDS sample code for java.util.concurrent.TimeUnit.MICROSECONDS definition code for java.util.concurrent.TimeUnit.MICROSECONDS
           
MILLISECONDS sample code for java.util.concurrent.TimeUnit.MILLISECONDS definition code for java.util.concurrent.TimeUnit.MILLISECONDS
           
NANOSECONDS sample code for java.util.concurrent.TimeUnit.NANOSECONDS definition code for java.util.concurrent.TimeUnit.NANOSECONDS
           
SECONDS sample code for java.util.concurrent.TimeUnit.SECONDS definition code for java.util.concurrent.TimeUnit.SECONDS
           
 
Method Summary
 long convert sample code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) (long duration, TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  unit)
          Convert the given time duration in the given unit to this unit.
 void sleep sample code for java.util.concurrent.TimeUnit.sleep(long) definition code for java.util.concurrent.TimeUnit.sleep(long) (long timeout)
          Perform a Thread.sleep using this unit.
 void timedJoin sample code for java.util.concurrent.TimeUnit.timedJoin(java.lang.Thread, long) definition code for java.util.concurrent.TimeUnit.timedJoin(java.lang.Thread, long) (Thread sample code for java.lang.Thread definition code for java.lang.Thread  thread, long timeout)
          Perform a timed Thread.join using this time unit.
 void timedWait sample code for java.util.concurrent.TimeUnit.timedWait(java.lang.Object, long) definition code for java.util.concurrent.TimeUnit.timedWait(java.lang.Object, long) (Object sample code for java.lang.Object definition code for java.lang.Object  obj, long timeout)
          Perform a timed Object.wait using this time unit.
 long toMicros sample code for java.util.concurrent.TimeUnit.toMicros(long) definition code for java.util.concurrent.TimeUnit.toMicros(long) (long duration)
          Equivalent to MICROSECONDS.convert(duration, this).
 long toMillis sample code for java.util.concurrent.TimeUnit.toMillis(long) definition code for java.util.concurrent.TimeUnit.toMillis(long) (long duration)
          Equivalent to MILLISECONDS.convert(duration, this).
 long toNanos sample code for java.util.concurrent.TimeUnit.toNanos(long) definition code for java.util.concurrent.TimeUnit.toNanos(long) (long duration)
          Equivalent to NANOSECONDS.convert(duration, this).
 long toSeconds sample code for java.util.concurrent.TimeUnit.toSeconds(long) definition code for java.util.concurrent.TimeUnit.toSeconds(long) (long duration)
          Equivalent to SECONDS.convert(duration, this).
static TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit valueOf sample code for java.util.concurrent.TimeUnit.valueOf(java.lang.String) definition code for java.util.concurrent.TimeUnit.valueOf(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  name)
          Returns the enum constant of this type with the specified name.
static TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit [] values sample code for java.util.concurrent.TimeUnit.values() definition code for java.util.concurrent.TimeUnit.values() ()
          Returns an array containing the constants of this enum type, in the order they're declared.
 
Methods inherited from class java.lang.Enum sample code for java.lang.Enum definition code for java.lang.Enum
clone sample code for java.lang.Enum.clone() definition code for java.lang.Enum.clone() , compareTo sample code for java.lang.Enum.compareTo(E) definition code for java.lang.Enum.compareTo(E) , equals sample code for java.lang.Enum.equals(java.lang.Object) definition code for java.lang.Enum.equals(java.lang.Object) , getDeclaringClass sample code for java.lang.Enum.getDeclaringClass() definition code for java.lang.Enum.getDeclaringClass() , hashCode sample code for java.lang.Enum.hashCode() definition code for java.lang.Enum.hashCode() , name sample code for java.lang.Enum.name() definition code for java.lang.Enum.name() , ordinal sample code for java.lang.Enum.ordinal() definition code for java.lang.Enum.ordinal() , toString sample code for java.lang.Enum.toString() definition code for java.lang.Enum.toString() , valueOf sample code for java.lang.Enum.valueOf(java.lang.Class, java.lang.String) definition code for java.lang.Enum.valueOf(java.lang.Class, java.lang.String)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Enum Constant Detail

NANOSECONDS

public static final TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  NANOSECONDS

MICROSECONDS

public static final TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  MICROSECONDS

MILLISECONDS

public static final TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  MILLISECONDS

SECONDS

public static final TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  SECONDS
Method Detail

values sample code for java.util.concurrent.TimeUnit.values() definition code for java.util.concurrent.TimeUnit.values()

public static final TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit [] values()
Returns an array containing the constants of this enum type, in the order they're declared. This method may be used to iterate over the constants as follows:
for(TimeUnit c : TimeUnit.values())
        System.out.println(c);

Returns:
an array containing the constants of this enum type, in the order they're declared

valueOf sample code for java.util.concurrent.TimeUnit.valueOf(java.lang.String) definition code for java.util.concurrent.TimeUnit.valueOf(java.lang.String)

public static TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  valueOf(String sample code for java.lang.String definition code for java.lang.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 sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if this enum type has no constant with the specified name

convert sample code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit)

public long convert(long duration,
                    TimeUnit sample code for java.util.concurrent.TimeUnit definition code for java.util.concurrent.TimeUnit  unit)
Convert the given time duration in the given unit to this unit. Conversions from finer to coarser granularities truncate, so lose precision. For example converting 999 milliseconds to seconds results in 0. Conversions from coarser to finer granularities with arguments that would numerically overflow saturate to Long.MIN_VALUE if negative or Long.MAX_VALUE if positive.

Parameters:
duration - the time duration in the given unit
unit - the unit of the duration argument
Returns:
the converted duration in this unit, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.

toNanos sample code for java.util.concurrent.TimeUnit.toNanos(long) definition code for java.util.concurrent.TimeUnit.toNanos(long)

public long toNanos(long duration)
Equivalent to NANOSECONDS.convert(duration, this).

Parameters:
duration - the duration
Returns:
the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also:
convert(long, java.util.concurrent.TimeUnit) sample code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit)

toMicros sample code for java.util.concurrent.TimeUnit.toMicros(long) definition code for java.util.concurrent.TimeUnit.toMicros(long)

public long toMicros(long duration)
Equivalent to MICROSECONDS.convert(duration, this).

Parameters:
duration - the duration
Returns:
the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also:
convert(long, java.util.concurrent.TimeUnit) sample code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit)

toMillis sample code for java.util.concurrent.TimeUnit.toMillis(long) definition code for java.util.concurrent.TimeUnit.toMillis(long)

public long toMillis(long duration)
Equivalent to MILLISECONDS.convert(duration, this).

Parameters:
duration - the duration
Returns:
the converted duration, or Long.MIN_VALUE if conversion would negatively overflow, or Long.MAX_VALUE if it would positively overflow.
See Also:
convert(long, java.util.concurrent.TimeUnit) sample code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit)

toSeconds sample code for java.util.concurrent.TimeUnit.toSeconds(long) definition code for java.util.concurrent.TimeUnit.toSeconds(long)

public long toSeconds(long duration)
Equivalent to SECONDS.convert(duration, this).

Parameters:
duration - the duration
Returns:
the converted duration.
See Also:
convert(long, java.util.concurrent.TimeUnit) sample code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.TimeUnit.convert(long, java.util.concurrent.TimeUnit)

timedWait sample code for java.util.concurrent.TimeUnit.timedWait(java.lang.Object, long) definition code for java.util.concurrent.TimeUnit.timedWait(java.lang.Object, long)

public void timedWait(Object sample code for java.lang.Object definition code for java.lang.Object  obj,
                      long timeout)
               throws InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException 
Perform a timed Object.wait using this time unit. This is a convenience method that converts timeout arguments into the form required by the Object.wait method.

For example, you could implement a blocking poll method (see BlockingQueue.poll sample code for java.util.concurrent.BlockingQueue.poll(long, java.util.concurrent.TimeUnit) definition code for java.util.concurrent.BlockingQueue.poll(long, java.util.concurrent.TimeUnit) ) using:

  public synchronized  Object poll(long timeout, TimeUnit unit) throws InterruptedException {
    while (empty) {
      unit.timedWait(this, timeout);
      ...
    }
  }

Parameters:
obj - the object to wait on
timeout - the maximum time to wait.
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if interrupted while waiting.
See Also:
Object.wait(long, int) sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)

timedJoin sample code for java.util.concurrent.TimeUnit.timedJoin(java.lang.Thread, long) definition code for java.util.concurrent.TimeUnit.timedJoin(java.lang.Thread, long)

public void timedJoin(Thread sample code for java.lang.Thread definition code for java.lang.Thread  thread,
                      long timeout)
               throws InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException 
Perform a timed Thread.join using this time unit. This is a convenience method that converts time arguments into the form required by the Thread.join method.

Parameters:
thread - the thread to wait for
timeout - the maximum time to wait
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if interrupted while waiting.
See Also:
Thread.join(long, int) sample code for java.lang.Thread.join(long, int) definition code for java.lang.Thread.join(long, int)

sleep sample code for java.util.concurrent.TimeUnit.sleep(long) definition code for java.util.concurrent.TimeUnit.sleep(long)

public void sleep(long timeout)
           throws InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException 
Perform a Thread.sleep using this unit. This is a convenience method that converts time arguments into the form required by the Thread.sleep method.

Parameters:
timeout - the minimum time to sleep
Throws:
InterruptedException sample code for java.lang.InterruptedException definition code for java.lang.InterruptedException - if interrupted while sleeping.
See Also:
Thread.sleep(long) sample code for java.lang.Thread.sleep(long) definition code for java.lang.Thread.sleep(long)