JavaTM 2 Platform
Standard Ed. 5.0

java.util.concurrent
枚举 TimeUnit

java.lang.Object
  继承者 java.lang.Enum<TimeUnit>
      继承者 java.util.concurrent.TimeUnit
所有已实现的接口:
Serializable, Comparable<TimeUnit>

public enum TimeUnit
extends Enum<TimeUnit>

TimeUnit 表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit 不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。

TimeUnit 主要用于通知基于时间的方法如何解释给定的计时参数。例如,如果 lock 不可用,则以下代码将在 50 毫秒后超时:

  Lock lock = ...;
  if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
 
而以下代码将在 50 秒后超时:
  Lock lock = ...;
  if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
 
但是注意,不保证特定超时实现能够以与给定 TimeUnit 相同的粒度通知 段。

从以下版本开始:
1.5

枚举常量摘要
MICROSECONDS
           
MILLISECONDS
           
NANOSECONDS
           
SECONDS
           
 
方法摘要
 long convert(long duration, TimeUnit unit)
          将给定单元的时间段转换到此单元。
 void sleep(long timeout)
          使用此单元执行 Thread.sleep.这是将时间参数转换为 Thread.sleep 方法所需格式的便捷方法。
 void timedJoin(Thread thread, long timeout)
          使用此时间单元执行计时的 Thread.join
 void timedWait(Object obj, long timeout)
          使用此时间单元执行计时的 Object.wait
 long toMicros(long duration)
          等效于 MICROSECONDS.convert(duration, this)
 long toMillis(long duration)
          等效于 MILLISECONDS.convert(duration, this)
 long toNanos(long duration)
          等效于 NANOSECONDS.convert(duration, this)
 long toSeconds(long duration)
          等效于 SECONDS.convert(duration, this)
static TimeUnit valueOf(String name)
          返回带有指定名称的该类型的枚举常量。
static TimeUnit[] values()
          按照声明该枚举类型的常量的顺序,返回 包含这些常量的数组。
 
从类 java.lang.Enum 继承的方法
clone, compareTo, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
 
从类 java.lang.Object 继承的方法
finalize, getClass, notify, notifyAll, wait, wait, wait
 

枚举常量详细信息

NANOSECONDS

public static final TimeUnit NANOSECONDS

MICROSECONDS

public static final TimeUnit MICROSECONDS

MILLISECONDS

public static final TimeUnit MILLISECONDS

SECONDS

public static final TimeUnit SECONDS
方法详细信息

values

public static final TimeUnit[] values()
按照声明该枚举类型的常量的顺序,返回 包含这些常量的数组。该方法可用于迭代 常量,如下所示:
for(TimeUnit c :TimeUnit.values())
        System.out.println(c);

返回:
按照声明该枚举类型的常量的顺序,返回 包含这些常量的数组。

valueOf

public static TimeUnit valueOf(String name)
返回带有指定名称的该类型的枚举常量。 字符串必须与用于声明该类型的枚举常量的 标识符完全匹配。(不允许有多余 的空格。)

参数:
指定要返回的枚举常量的名称。 -
返回:
返回带有指定名称的枚举常量
抛出:
如果该枚举类型没有带有指定名称的常量, - 则抛出 IllegalArgumentException

convert

public long convert(long duration,
                    TimeUnit unit)
将给定单元的时间段转换到此单元。从较细粒度到较粗粒度的舍位转换,这样会失去精确性。例如,将 999 毫秒转换为秒的结果为 0。使用参数从较粗粒度到较细粒度转换,如果参数为负,则在数字上溢出至 Long.MIN_VALUE,如果为正,则为 Long.MAX_VALUE

参数:
duration - 给定 unit 中的时间段
unit - duration 参数的单元
返回:
此单元中的转换时间段;如果转换将负溢出,则返回 Long.MIN_VALUE;如果转换将正溢出,则返回 Long.MAX_VALUE

toNanos

public long toNanos(long duration)
等效于 NANOSECONDS.convert(duration, this)

参数:
duration - 时间段
返回:
转换时间段,如果转换将负溢出,则返回 Long.MIN_VALUE;或者如果转换将正溢出,则返回 Long.MAX_VALUE
另请参见:
convert(long, java.util.concurrent.TimeUnit)

toMicros

public long toMicros(long duration)
等效于 MICROSECONDS.convert(duration, this)

参数:
duration - 时间段
返回:
转换时间段,如果转换将负溢出,则返回 Long.MIN_VALUE;或者如果转换将正溢出,则返回 Long.MAX_VALUE
另请参见:
convert(long, java.util.concurrent.TimeUnit)

toMillis

public long toMillis(long duration)
等效于 MILLISECONDS.convert(duration, this)

参数:
duration - 时间段
返回:
转换时间段,如果转换将负溢出,则返回 Long.MIN_VALUE;或者如果转换将正溢出,则返回 Long.MAX_VALUE
另请参见:
convert(long, java.util.concurrent.TimeUnit)

toSeconds

public long toSeconds(long duration)
等效于 SECONDS.convert(duration, this)

参数:
duration - 时间段
返回:
转换时间段。
另请参见:
convert(long, java.util.concurrent.TimeUnit)

timedWait

public void timedWait(Object obj,
                      long timeout)
               throws InterruptedException
使用此时间单元执行计时的 Object.wait。这是将超时参数转换为 Object.wait 方法所需格式的便捷方法。

例如,可以使用以下代码实现阻塞 poll 方法(参见 BlockingQueue.poll):

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

参数:
obj - 要等待的对象
timeout - 要等待的最长时间。
抛出:
InterruptedException - 如果等待时中断。
另请参见:
Object.wait(long, int)

timedJoin

public void timedJoin(Thread thread,
                      long timeout)
               throws InterruptedException
使用此时间单元执行计时的 Thread.join。这是将时间参数转换为 Thread.join 方法所需格式的便捷方法。

参数:
thread - 要等待的线程
timeout - 要等待的最长时间
抛出:
InterruptedException - 如果等待时中断。
另请参见:
Thread.join(long, int)

sleep

public void sleep(long timeout)
           throws InterruptedException
使用此单元执行 Thread.sleep.这是将时间参数转换为 Thread.sleep 方法所需格式的便捷方法。

参数:
timeout - 休眠的最短时间
抛出:
InterruptedException - 如果休眠时中断。
另请参见:
Thread.sleep(long)

JavaTM 2 Platform
Standard Ed. 5.0

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策