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

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

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

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

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

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