Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

edu.emory.mathcs.util.concurrent
Class TimeUnit  view TimeUnit download TimeUnit.java

java.lang.Object
  extended byedu.emory.mathcs.util.concurrent.TimeUnit
All Implemented Interfaces:
java.io.Serializable

public final class TimeUnit
extends java.lang.Object
implements java.io.Serializable

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. TimeUnit is a "featherweight" class. It does not maintain time information, but only helps organize and use time representations that may be maintained separately across various contexts.

The TimeUnit class cannot be directly instantiated. Use the SECONDS 55 , MILLISECONDS 55 , MICROSECONDS 55 , and NANOSECONDS 55 static instances that provide predefined units of precision. If you use these frequently, consider statically importing this class.

A TimeUnit is mainly used to inform blocking methods which can timeout, how the timeout parameter should be interpreted. For example, the following code will timeout in 50 milliseconds if the 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 lock, in this case, will be able to notice the passage of time at the same granularity as the given TimeUnit.

Since:
1.5

Field Summary
(package private)  int index
          the index of this unit
static TimeUnit MICROSECONDS
          Unit for one-microsecond granularities
static TimeUnit MILLISECONDS
          Unit for one-millisecond granularities
private static int MS
           
(package private) static int[] multipliers
          quick lookup table for conversion factors
static TimeUnit NANOSECONDS
          Unit for one-nanosecond granularities
private static int NS
           
private static int S
           
static TimeUnit SECONDS
          Unit for one-second granularities
private static int US
           
 
Constructor Summary
(package private) TimeUnit(int index)
          private constructor
 
Method Summary
 long convert(long duration, TimeUnit unit)
          Convert the given time duration in the given unit to the current unit.
private  int excessNanos(long time, long ms)
          Utility method to compute the excess-nanosecond argument to wait, sleep, join.
 void sleep(long timeout)
          Perform a Thread.sleep using the current time unit.
 void timedJoin(java.lang.Thread thread, long timeout)
          Perform a timed Thread.join using the current time unit.
 void timedWait(java.lang.Object obj, long timeout)
          Perform a timed Object.wait using the current time unit.
 long toNanos(long duration)
          Equivalent to NANOSECONDS.convert(duration, this).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NS

private static final int NS
See Also:
Constant Field Values

US

private static final int US
See Also:
Constant Field Values

MS

private static final int MS
See Also:
Constant Field Values

S

private static final int S
See Also:
Constant Field Values

multipliers

static final int[] multipliers
quick lookup table for conversion factors


index

int index
the index of this unit


SECONDS

public static final TimeUnit SECONDS
Unit for one-second granularities


MILLISECONDS

public static final TimeUnit MILLISECONDS
Unit for one-millisecond granularities


MICROSECONDS

public static final TimeUnit MICROSECONDS
Unit for one-microsecond granularities


NANOSECONDS

public static final TimeUnit NANOSECONDS
Unit for one-nanosecond granularities

Constructor Detail

TimeUnit

TimeUnit(int index)
private constructor

Method Detail

convert

public long convert(long duration,
                    TimeUnit unit)
Convert the given time duration in the given unit to the current unit. Conversions from finer to coarser granulaties truncate, so lose precision. Conversions from coarser to finer granularities may numerically overflow.


toNanos

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


timedWait

public void timedWait(java.lang.Object obj,
                      long timeout)
               throws java.lang.InterruptedException
Perform a timed Object.wait using the current 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 55 using:

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


timedJoin

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


sleep

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


excessNanos

private int excessNanos(long time,
                        long ms)
Utility method to compute the excess-nanosecond argument to wait, sleep, join.