jaroslav@633: /* jaroslav@633: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@633: * jaroslav@633: * This code is free software; you can redistribute it and/or modify it jaroslav@633: * under the terms of the GNU General Public License version 2 only, as jaroslav@633: * published by the Free Software Foundation. Oracle designates this jaroslav@633: * particular file as subject to the "Classpath" exception as provided jaroslav@633: * by Oracle in the LICENSE file that accompanied this code. jaroslav@633: * jaroslav@633: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@633: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@633: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@633: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@633: * accompanied this code). jaroslav@633: * jaroslav@633: * You should have received a copy of the GNU General Public License version jaroslav@633: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@633: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@633: * jaroslav@633: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@633: * or visit www.oracle.com if you need additional information or have any jaroslav@633: * questions. jaroslav@633: */ jaroslav@633: jaroslav@633: /* jaroslav@633: * This file is available under and governed by the GNU General Public jaroslav@633: * License version 2 only, as published by the Free Software Foundation. jaroslav@633: * However, the following notice accompanied the original version of this jaroslav@633: * file: jaroslav@633: * jaroslav@633: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@633: * Expert Group and released to the public domain, as explained at jaroslav@633: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@633: */ jaroslav@633: jaroslav@633: package java.util.concurrent; jaroslav@633: jaroslav@633: /** jaroslav@633: * A TimeUnit represents time durations at a given unit of jaroslav@633: * granularity and provides utility methods to convert across units, jaroslav@633: * and to perform timing and delay operations in these units. A jaroslav@633: * TimeUnit does not maintain time information, but only jaroslav@633: * helps organize and use time representations that may be maintained jaroslav@633: * separately across various contexts. A nanosecond is defined as one jaroslav@633: * thousandth of a microsecond, a microsecond as one thousandth of a jaroslav@633: * millisecond, a millisecond as one thousandth of a second, a minute jaroslav@633: * as sixty seconds, an hour as sixty minutes, and a day as twenty four jaroslav@633: * hours. jaroslav@633: * jaroslav@633: *

A TimeUnit is mainly used to inform time-based methods jaroslav@633: * how a given timing parameter should be interpreted. For example, jaroslav@633: * the following code will timeout in 50 milliseconds if the {@link jaroslav@633: * java.util.concurrent.locks.Lock lock} is not available: jaroslav@633: * jaroslav@633: *

  Lock lock = ...;
jaroslav@633:  *  if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...
jaroslav@633:  * 
jaroslav@633: * while this code will timeout in 50 seconds: jaroslav@633: *
jaroslav@633:  *  Lock lock = ...;
jaroslav@633:  *  if (lock.tryLock(50L, TimeUnit.SECONDS)) ...
jaroslav@633:  * 
jaroslav@633: * jaroslav@633: * Note however, that there is no guarantee that a particular timeout jaroslav@633: * implementation will be able to notice the passage of time at the jaroslav@633: * same granularity as the given TimeUnit. jaroslav@633: * jaroslav@633: * @since 1.5 jaroslav@633: * @author Doug Lea jaroslav@633: */ jaroslav@633: public enum TimeUnit { jaroslav@633: NANOSECONDS { jaroslav@633: public long toNanos(long d) { return d; } jaroslav@633: public long toMicros(long d) { return d/(C1/C0); } jaroslav@633: public long toMillis(long d) { return d/(C2/C0); } jaroslav@633: public long toSeconds(long d) { return d/(C3/C0); } jaroslav@633: public long toMinutes(long d) { return d/(C4/C0); } jaroslav@633: public long toHours(long d) { return d/(C5/C0); } jaroslav@633: public long toDays(long d) { return d/(C6/C0); } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toNanos(d); } jaroslav@633: int excessNanos(long d, long m) { return (int)(d - (m*C2)); } jaroslav@633: }, jaroslav@633: MICROSECONDS { jaroslav@633: public long toNanos(long d) { return x(d, C1/C0, MAX/(C1/C0)); } jaroslav@633: public long toMicros(long d) { return d; } jaroslav@633: public long toMillis(long d) { return d/(C2/C1); } jaroslav@633: public long toSeconds(long d) { return d/(C3/C1); } jaroslav@633: public long toMinutes(long d) { return d/(C4/C1); } jaroslav@633: public long toHours(long d) { return d/(C5/C1); } jaroslav@633: public long toDays(long d) { return d/(C6/C1); } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toMicros(d); } jaroslav@633: int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); } jaroslav@633: }, jaroslav@633: MILLISECONDS { jaroslav@633: public long toNanos(long d) { return x(d, C2/C0, MAX/(C2/C0)); } jaroslav@633: public long toMicros(long d) { return x(d, C2/C1, MAX/(C2/C1)); } jaroslav@633: public long toMillis(long d) { return d; } jaroslav@633: public long toSeconds(long d) { return d/(C3/C2); } jaroslav@633: public long toMinutes(long d) { return d/(C4/C2); } jaroslav@633: public long toHours(long d) { return d/(C5/C2); } jaroslav@633: public long toDays(long d) { return d/(C6/C2); } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toMillis(d); } jaroslav@633: int excessNanos(long d, long m) { return 0; } jaroslav@633: }, jaroslav@633: SECONDS { jaroslav@633: public long toNanos(long d) { return x(d, C3/C0, MAX/(C3/C0)); } jaroslav@633: public long toMicros(long d) { return x(d, C3/C1, MAX/(C3/C1)); } jaroslav@633: public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); } jaroslav@633: public long toSeconds(long d) { return d; } jaroslav@633: public long toMinutes(long d) { return d/(C4/C3); } jaroslav@633: public long toHours(long d) { return d/(C5/C3); } jaroslav@633: public long toDays(long d) { return d/(C6/C3); } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toSeconds(d); } jaroslav@633: int excessNanos(long d, long m) { return 0; } jaroslav@633: }, jaroslav@633: MINUTES { jaroslav@633: public long toNanos(long d) { return x(d, C4/C0, MAX/(C4/C0)); } jaroslav@633: public long toMicros(long d) { return x(d, C4/C1, MAX/(C4/C1)); } jaroslav@633: public long toMillis(long d) { return x(d, C4/C2, MAX/(C4/C2)); } jaroslav@633: public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); } jaroslav@633: public long toMinutes(long d) { return d; } jaroslav@633: public long toHours(long d) { return d/(C5/C4); } jaroslav@633: public long toDays(long d) { return d/(C6/C4); } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toMinutes(d); } jaroslav@633: int excessNanos(long d, long m) { return 0; } jaroslav@633: }, jaroslav@633: HOURS { jaroslav@633: public long toNanos(long d) { return x(d, C5/C0, MAX/(C5/C0)); } jaroslav@633: public long toMicros(long d) { return x(d, C5/C1, MAX/(C5/C1)); } jaroslav@633: public long toMillis(long d) { return x(d, C5/C2, MAX/(C5/C2)); } jaroslav@633: public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); } jaroslav@633: public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); } jaroslav@633: public long toHours(long d) { return d; } jaroslav@633: public long toDays(long d) { return d/(C6/C5); } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toHours(d); } jaroslav@633: int excessNanos(long d, long m) { return 0; } jaroslav@633: }, jaroslav@633: DAYS { jaroslav@633: public long toNanos(long d) { return x(d, C6/C0, MAX/(C6/C0)); } jaroslav@633: public long toMicros(long d) { return x(d, C6/C1, MAX/(C6/C1)); } jaroslav@633: public long toMillis(long d) { return x(d, C6/C2, MAX/(C6/C2)); } jaroslav@633: public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); } jaroslav@633: public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); } jaroslav@633: public long toHours(long d) { return x(d, C6/C5, MAX/(C6/C5)); } jaroslav@633: public long toDays(long d) { return d; } jaroslav@633: public long convert(long d, TimeUnit u) { return u.toDays(d); } jaroslav@633: int excessNanos(long d, long m) { return 0; } jaroslav@633: }; jaroslav@633: jaroslav@633: // Handy constants for conversion methods jaroslav@633: static final long C0 = 1L; jaroslav@633: static final long C1 = C0 * 1000L; jaroslav@633: static final long C2 = C1 * 1000L; jaroslav@633: static final long C3 = C2 * 1000L; jaroslav@633: static final long C4 = C3 * 60L; jaroslav@633: static final long C5 = C4 * 60L; jaroslav@633: static final long C6 = C5 * 24L; jaroslav@633: jaroslav@633: static final long MAX = Long.MAX_VALUE; jaroslav@633: jaroslav@633: /** jaroslav@633: * Scale d by m, checking for overflow. jaroslav@633: * This has a short name to make above code more readable. jaroslav@633: */ jaroslav@633: static long x(long d, long m, long over) { jaroslav@633: if (d > over) return Long.MAX_VALUE; jaroslav@633: if (d < -over) return Long.MIN_VALUE; jaroslav@633: return d * m; jaroslav@633: } jaroslav@633: jaroslav@633: // To maintain full signature compatibility with 1.5, and to improve the jaroslav@633: // clarity of the generated javadoc (see 6287639: Abstract methods in jaroslav@633: // enum classes should not be listed as abstract), method convert jaroslav@633: // etc. are not declared abstract but otherwise act as abstract methods. jaroslav@633: jaroslav@633: /** jaroslav@633: * Convert the given time duration in the given unit to this jaroslav@633: * unit. Conversions from finer to coarser granularities jaroslav@633: * truncate, so lose precision. For example converting jaroslav@633: * 999 milliseconds to seconds results in jaroslav@633: * 0. Conversions from coarser to finer granularities jaroslav@633: * with arguments that would numerically overflow saturate to jaroslav@633: * Long.MIN_VALUE if negative or Long.MAX_VALUE jaroslav@633: * if positive. jaroslav@633: * jaroslav@633: *

For example, to convert 10 minutes to milliseconds, use: jaroslav@633: * TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES) jaroslav@633: * jaroslav@633: * @param sourceDuration the time duration in the given sourceUnit jaroslav@633: * @param sourceUnit the unit of the sourceDuration argument jaroslav@633: * @return the converted duration in this unit, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: */ jaroslav@633: public long convert(long sourceDuration, TimeUnit sourceUnit) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to NANOSECONDS.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: * @see #convert jaroslav@633: */ jaroslav@633: public long toNanos(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to MICROSECONDS.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: * @see #convert jaroslav@633: */ jaroslav@633: public long toMicros(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to MILLISECONDS.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: * @see #convert jaroslav@633: */ jaroslav@633: public long toMillis(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to SECONDS.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: * @see #convert jaroslav@633: */ jaroslav@633: public long toSeconds(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to MINUTES.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: * @see #convert jaroslav@633: * @since 1.6 jaroslav@633: */ jaroslav@633: public long toMinutes(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to HOURS.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration, jaroslav@633: * or Long.MIN_VALUE if conversion would negatively jaroslav@633: * overflow, or Long.MAX_VALUE if it would positively overflow. jaroslav@633: * @see #convert jaroslav@633: * @since 1.6 jaroslav@633: */ jaroslav@633: public long toHours(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Equivalent to DAYS.convert(duration, this). jaroslav@633: * @param duration the duration jaroslav@633: * @return the converted duration jaroslav@633: * @see #convert jaroslav@633: * @since 1.6 jaroslav@633: */ jaroslav@633: public long toDays(long duration) { jaroslav@633: throw new AbstractMethodError(); jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Utility to compute the excess-nanosecond argument to wait, jaroslav@633: * sleep, join. jaroslav@633: * @param d the duration jaroslav@633: * @param m the number of milliseconds jaroslav@633: * @return the number of nanoseconds jaroslav@633: */ jaroslav@633: abstract int excessNanos(long d, long m); jaroslav@633: jaroslav@633: /** jaroslav@633: * Performs a timed {@link Object#wait(long, int) Object.wait} jaroslav@633: * using this time unit. jaroslav@633: * This is a convenience method that converts timeout arguments jaroslav@633: * into the form required by the Object.wait method. jaroslav@633: * jaroslav@633: *

For example, you could implement a blocking poll jaroslav@633: * method (see {@link BlockingQueue#poll BlockingQueue.poll}) jaroslav@633: * using: jaroslav@633: * jaroslav@633: *

 {@code
jaroslav@633:      * public synchronized Object poll(long timeout, TimeUnit unit)
jaroslav@633:      *     throws InterruptedException {
jaroslav@633:      *   while (empty) {
jaroslav@633:      *     unit.timedWait(this, timeout);
jaroslav@633:      *     ...
jaroslav@633:      *   }
jaroslav@633:      * }}
jaroslav@633: * jaroslav@633: * @param obj the object to wait on jaroslav@633: * @param timeout the maximum time to wait. If less than jaroslav@633: * or equal to zero, do not wait at all. jaroslav@633: * @throws InterruptedException if interrupted while waiting jaroslav@633: */ jaroslav@633: public void timedWait(Object obj, long timeout) jaroslav@633: throws InterruptedException { jaroslav@633: if (timeout > 0) { jaroslav@633: long ms = toMillis(timeout); jaroslav@633: int ns = excessNanos(timeout, ms); jaroslav@633: obj.wait(ms, ns); jaroslav@633: } jaroslav@633: } jaroslav@633: jaroslav@633: /** jaroslav@633: * Performs a timed {@link Thread#join(long, int) Thread.join} jaroslav@633: * using this time unit. jaroslav@633: * This is a convenience method that converts time arguments into the jaroslav@633: * form required by the Thread.join method. jaroslav@633: * jaroslav@633: * @param thread the thread to wait for jaroslav@633: * @param timeout the maximum time to wait. If less than jaroslav@633: * or equal to zero, do not wait at all. jaroslav@633: * @throws InterruptedException if interrupted while waiting jaroslav@633: */ jaroslav@635: // public void timedJoin(Thread thread, long timeout) jaroslav@635: // throws InterruptedException { jaroslav@635: // if (timeout > 0) { jaroslav@635: // long ms = toMillis(timeout); jaroslav@635: // int ns = excessNanos(timeout, ms); jaroslav@635: // thread.join(ms, ns); jaroslav@635: // } jaroslav@635: // } jaroslav@633: jaroslav@633: /** jaroslav@633: * Performs a {@link Thread#sleep(long, int) Thread.sleep} using jaroslav@633: * this time unit. jaroslav@633: * This is a convenience method that converts time arguments into the jaroslav@633: * form required by the Thread.sleep method. jaroslav@633: * jaroslav@633: * @param timeout the minimum time to sleep. If less than jaroslav@633: * or equal to zero, do not sleep at all. jaroslav@633: * @throws InterruptedException if interrupted while sleeping jaroslav@633: */ jaroslav@633: public void sleep(long timeout) throws InterruptedException { jaroslav@633: if (timeout > 0) { jaroslav@633: long ms = toMillis(timeout); jaroslav@633: int ns = excessNanos(timeout, ms); jaroslav@635: Object o = new Object(); jaroslav@635: synchronized (o) { jaroslav@635: o.wait(ms, ns); jaroslav@635: } jaroslav@633: } jaroslav@633: } jaroslav@633: jaroslav@633: }