| 
 | JavaTM 2 Platform Standard Ed. 5.0 | |||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 枚举常量 | 字段 | 方法 | 详细信息: 枚举常量 | 字段 | 方法 | |||||||||
java.lang.Objectjava.lang.Enum<TimeUnit>
java.util.concurrent.TimeUnit
public 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 相同的粒度通知 段。
| 枚举常量摘要 | |
|---|---|
| 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 | 
| 枚举常量详细信息 | 
|---|
public static final TimeUnit NANOSECONDS
public static final TimeUnit MICROSECONDS
public static final TimeUnit MILLISECONDS
public static final TimeUnit SECONDS
| 方法详细信息 | 
|---|
public static final TimeUnit[] values()
for(TimeUnit c :TimeUnit.values())
        System.out.println(c);
public static TimeUnit valueOf(String name)
指定要返回的枚举常量的名称。 - 
如果该枚举类型没有带有指定名称的常量, - 则抛出 IllegalArgumentException
public long convert(long duration,
                    TimeUnit unit)
duration - 给定 unit 中的时间段unit - duration 参数的单元
public long toNanos(long duration)
duration - 时间段
convert(long, java.util.concurrent.TimeUnit)public long toMicros(long duration)
duration - 时间段
convert(long, java.util.concurrent.TimeUnit)public long toMillis(long duration)
duration - 时间段
convert(long, java.util.concurrent.TimeUnit)public long toSeconds(long duration)
duration - 时间段
convert(long, java.util.concurrent.TimeUnit)
public void timedWait(Object obj,
                      long timeout)
               throws InterruptedException
例如,可以使用以下代码实现阻塞 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)
public void timedJoin(Thread thread,
                      long timeout)
               throws InterruptedException
thread - 要等待的线程timeout - 要等待的最长时间
InterruptedException - 如果等待时中断。Thread.join(long, int)
public void sleep(long timeout)
           throws InterruptedException
timeout - 休眠的最短时间
InterruptedException - 如果休眠时中断。Thread.sleep(long)| 
 | JavaTM 2 Platform Standard Ed. 5.0 | |||||||||
| 上一个类 下一个类 | 框架 无框架 | |||||||||
| 摘要: 嵌套 | 枚举常量 | 字段 | 方法 | 详细信息: 枚举常量 | 字段 | 方法 | |||||||||
版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策。