| 
 | JavaTM 2 Platform Standard Ed. 5.0 | |||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | 列挙型定数 | フィールド | メソッド | 詳細: 列挙型定数 | フィールド | メソッド | |||||||||
java.lang.Objectjava.lang.Enum<TimeUnit>
java.util.concurrent.TimeUnit
public enum TimeUnit
TimeUnit は、期間を指定された粒度単位で表します。これは、単位間の変換を行い、これらの単位でタイミングおよび遅延操作を実行するユーティリティメソッドを提供します。TimeUnit は時間情報を管理しませんが、さまざまなコンテキスト間で別個に管理される時間表現を整理および使用するのに役立ちます。
TimeUnit は、主に時間ベースのメソッドに、指定されたタイミングパラメータを解釈する方法を指示する場合に使用します。たとえば次のコードは、ロックが使用できない場合、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 を実行します。 | 
|  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)
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 | |||||||||
| 前のクラス 次のクラス | フレームあり フレームなし | |||||||||
| 概要: 入れ子 | 列挙型定数 | フィールド | メソッド | 詳細: 列挙型定数 | フィールド | メソッド | |||||||||
Copyright 2004 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Documentation Redistribution Policy も参照してください。