--- jsr166/src/main/java/util/concurrent/TimeUnit.java 2003/07/12 00:50:34 1.5 +++ jsr166/src/main/java/util/concurrent/TimeUnit.java 2005/04/26 01:17:18 1.29 @@ -1,174 +1,316 @@ /* - * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain. Use, modify, and - * redistribute this code in any way without acknowledgement. - */ + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + */ package java.util.concurrent; /** - * 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. + * 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 nanosecond is defined as one + * thousandth of a microsecond, a microsecond as one thousandth of a + * millisecond, a millisecond as one thousandth of a second, a minute + * as sixty seconds, an hour as sixty minutes, and a day as twenty four + * hours. * - *

The TimeUnit class cannot be directly instantiated. - * Use the {@link #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, - * and {@link #NANOSECONDS} 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 time-based methods + * how a given timing parameter should be interpreted. For example, + * the following code will timeout in 50 milliseconds if the {@link + * java.util.concurrent.locks.Lock lock} is not available: * - *

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 {@link Lock 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. + * + * 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 - * @spec JSR-166 - * @revised $Date: 2003/07/12 00:50:34 $ - * @editor $Author: dl $ * @author Doug Lea */ -public final class TimeUnit implements java.io.Serializable { +public enum TimeUnit { + NANOSECONDS (0) { + public long toNanos(long d) { return d; } + public long toMicros(long d) { return d/(C1/C0); } + public long toMillis(long d) { return d/(C2/C0); } + public long toSeconds(long d) { return d/(C3/C0); } + public long toMinutes(long d) { return d/(C4/C0); } + public long toHours(long d) { return d/(C5/C0); } + public long toDays(long d) { return d/(C6/C0); } + public long convert(long d, TimeUnit u) { return u.toNanos(d); } + int excessNanos(long d, long m) { return (int)(d - (m*C2)); } + }, + MICROSECONDS (1) { + public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); } + public long toMicros(long d) { return d; } + public long toMillis(long d) { return d/(C2/C1); } + public long toSeconds(long d) { return d/(C3/C1); } + public long toMinutes(long d) { return d/(C4/C1); } + public long toHours(long d) { return d/(C5/C1); } + public long toDays(long d) { return d/(C6/C1); } + public long convert(long d, TimeUnit u) { return u.toMicros(d); } + int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); } + }, + MILLISECONDS (2) { + public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } + public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); } + public long toMillis(long d) { return d; } + public long toSeconds(long d) { return d/(C3/C2); } + public long toMinutes(long d) { return d/(C4/C2); } + public long toHours(long d) { return d/(C5/C2); } + public long toDays(long d) { return d/(C6/C2); } + public long convert(long d, TimeUnit u) { return u.toMillis(d); } + int excessNanos(long d, long m) { return 0; } + }, + SECONDS (3) { + public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); } + public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); } + public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); } + public long toSeconds(long d) { return d; } + public long toMinutes(long d) { return d/(C4/C3); } + public long toHours(long d) { return d/(C5/C3); } + public long toDays(long d) { return d/(C6/C3); } + public long convert(long d, TimeUnit u) { return u.toSeconds(d); } + int excessNanos(long d, long m) { return 0; } + }, + MINUTES (4) { + public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); } + public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); } + public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); } + public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); } + public long toMinutes(long d) { return d; } + public long toHours(long d) { return d/(C5/C4); } + public long toDays(long d) { return d/(C6/C4); } + public long convert(long d, TimeUnit u) { return u.toMinutes(d); } + int excessNanos(long d, long m) { return 0; } + }, + HOURS (5) { + public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); } + public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); } + public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); } + public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); } + public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); } + public long toHours(long d) { return d; } + public long toDays(long d) { return d/(C6/C5); } + public long convert(long d, TimeUnit u) { return u.toHours(d); } + int excessNanos(long d, long m) { return 0; } + }, + DAYS (6) { + public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); } + public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); } + public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); } + public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); } + public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); } + public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); } + public long toDays(long d) { return d; } + public long convert(long d, TimeUnit u) { return u.toDays(d); } + int excessNanos(long d, long m) { return 0; } + }; + + /** + * The index of this unit. This value is no longer used in this + * version of this class, but is retained for serialization + * compatibility with previous version. + */ + private final int index; + + /** Internal constructor */ + TimeUnit(int index) { + this.index = index; + } + + // Handy constants for conversion methods + static final long C0 = 1L; + static final long C1 = C0 * 1000L; + static final long C2 = C1 * 1000L; + static final long C3 = C2 * 1000L; + static final long C4 = C3 * 60L; + static final long C5 = C4 * 60L; + static final long C6 = C5 * 24L; + + static final long MAX = Long.MAX_VALUE; /** - * 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. + * Scale d by m, checking for overflow. + * This has a short name to make above code more readable. + */ + static long x(long d, long m, long over) { + if (d > over) return Long.MAX_VALUE; + if (d < -over) return Long.MIN_VALUE; + return d * m; + } + + /** + * 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. * * @param duration the time duration in the given unit * @param unit the unit of the duration argument - * @return the converted duration in the current unit. + * @return the converted duration in this unit, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. */ - public long convert(long duration, TimeUnit unit) { - if (unit == this) - return duration; - if (index > unit.index) - return duration / multipliers[index - unit.index]; - else - return duration * multipliers[unit.index - index]; - } + public abstract long convert(long duration, TimeUnit unit); /** - * Equivalent to NANOSECONDS.convert(duration, this). + * Equivalent to NANOSECONDS.convert(duration, this). * @param duration the duration - * @return the converted duration. - **/ - public long toNanos(long duration) { - if (index == NS) - return duration; - else - return duration * multipliers[index]; - } - + * @return the converted duration, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. + * @see #convert + */ + public abstract long toNanos(long duration); + + /** + * Equivalent to MICROSECONDS.convert(duration, this). + * @param duration the duration + * @return the converted duration, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. + * @see #convert + */ + public abstract long toMicros(long duration); + + /** + * Equivalent to MILLISECONDS.convert(duration, this). + * @param duration the duration + * @return the converted duration, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. + * @see #convert + */ + public abstract long toMillis(long duration); + + /** + * Equivalent to SECONDS.convert(duration, this). + * @param duration the duration + * @return the converted duration, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. + * @see #convert + */ + public abstract long toSeconds(long duration); + + /** + * Equivalent to MINUTES.convert(duration, this). + * @param duration the duration + * @return the converted duration, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. + * @see #convert + */ + public abstract long toMinutes(long duration); + + /** + * Equivalent to HOURS.convert(duration, this). + * @param duration the duration + * @return the converted duration, + * or Long.MIN_VALUE if conversion would negatively + * overflow, or Long.MAX_VALUE if it would positively overflow. + * @see #convert + */ + public abstract long toHours(long duration); + + /** + * Equivalent to DAYS.convert(duration, this). + * @param duration the duration + * @return the converted duration + * @see #convert + */ + public abstract long toDays(long duration); + /** - * 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 - * {@link BlockingQueue#poll BlockingQueue.poll} using: + * Utility to compute the excess-nanosecond argument to wait, + * sleep, join. + * @param d the duration + * @param m the number of milliseconds + * @return the number of nanoseconds + */ + abstract int excessNanos(long d, long m); + + /** + * Performs 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 {@link BlockingQueue#poll BlockingQueue.poll}) + * using: + * *

  public synchronized  Object poll(long timeout, TimeUnit unit) throws InterruptedException {
      *    while (empty) {
      *      unit.timedWait(this, timeout);
      *      ...
      *    }
      *  }
+ * * @param obj the object to wait on - * @param timeout the maximum time to wait + * @param timeout the maximum time to wait. If less than + * or equal to zero, do not wait at all. * @throws InterruptedException if interrupted while waiting. * @see Object#wait(long, int) */ public void timedWait(Object obj, long timeout) - throws InterruptedException { - long ms = MILLISECONDS.convert(timeout, this); - int ns = excessNanos(timeout, ms); - obj.wait(ms, ns); + throws InterruptedException { + if (timeout > 0) { + long ms = toMillis(timeout); + int ns = excessNanos(timeout, ms); + obj.wait(ms, ns); + } } /** - * Perform a timed Thread.join using the current time unit. + * Performs 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. * @param thread the thread to wait for - * @param timeout the maximum time to wait + * @param timeout the maximum time to wait. If less than + * or equal to zero, do not wait at all. * @throws InterruptedException if interrupted while waiting. * @see Thread#join(long, int) */ public void timedJoin(Thread thread, long timeout) - throws InterruptedException { - long ms = MILLISECONDS.convert(timeout, this); - int ns = excessNanos(timeout, ms); - thread.join(ms, ns); + throws InterruptedException { + if (timeout > 0) { + long ms = toMillis(timeout); + int ns = excessNanos(timeout, ms); + thread.join(ms, ns); + } } - + /** - * Perform a Thread.sleep using the current time unit. + * Performs a Thread.sleep using this unit. * This is a convenience method that converts time arguments into the * form required by the Thread.sleep method. - * @param timeout the minimum time to sleep + * @param timeout the minimum time to sleep. If less than + * or equal to zero, do not sleep at all. * @throws InterruptedException if interrupted while sleeping. * @see Thread#sleep */ public void sleep(long timeout) throws InterruptedException { - long ms = MILLISECONDS.convert(timeout, this); - int ns = excessNanos(timeout, ms); - Thread.sleep(ms, ns); + if (timeout > 0) { + long ms = toMillis(timeout); + int ns = excessNanos(timeout, ms); + Thread.sleep(ms, ns); + } } - /* ordered indices for each time unit */ - private static final int NS = 0; - private static final int US = 1; - private static final int MS = 2; - private static final int S = 3; - - /** quick lookup table for conversion factors */ - static final int[] multipliers = { 1, 1000, 1000*1000, 1000*1000*1000 }; - - /** the index of this unit */ - int index; - - /** private constructor */ - TimeUnit(int index) { this.index = index; } - - /** - * Utility method to compute the excess-nanosecond argument to - * wait, sleep, join. - * @fixme overflow? - */ - private int excessNanos(long time, long ms) { - if (index == NS) - return (int) (time - (ms * multipliers[MS-NS])); - else if (index == US) - return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS])); - else - return 0; - } - - /** Unit for one-second granularities */ - public static final TimeUnit SECONDS = new TimeUnit(S); - - /** Unit for one-millisecond granularities */ - public static final TimeUnit MILLISECONDS = new TimeUnit(MS); - - /** Unit for one-microsecond granularities */ - public static final TimeUnit MICROSECONDS = new TimeUnit(US); - - /** Unit for one-nanosecond granularities */ - public static final TimeUnit NANOSECONDS = new TimeUnit(NS); - }