View Javadoc
1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3    *
4    * This code is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License version 2 only, as
6    * published by the Free Software Foundation.  Oracle designates this
7    * particular file as subject to the "Classpath" exception as provided
8    * by Oracle in the LICENSE file that accompanied this code.
9    *
10   * This code is distributed in the hope that it will be useful, but WITHOUT
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13   * version 2 for more details (a copy is included in the LICENSE file that
14   * accompanied this code).
15   *
16   * You should have received a copy of the GNU General Public License version
17   * 2 along with this work; if not, write to the Free Software Foundation,
18   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19   *
20   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21   * or visit www.oracle.com if you need additional information or have any
22   * questions.
23   */
24  
25  /*
26   * This file is available under and governed by the GNU General Public
27   * License version 2 only, as published by the Free Software Foundation.
28   * However, the following notice accompanied the original version of this
29   * file:
30   *
31   * Written by Doug Lea with assistance from members of JCP JSR-166
32   * Expert Group and released to the public domain, as explained at
33   * http://creativecommons.org/publicdomain/zero/1.0/
34   */
35  
36  package java.util.concurrent;
37  
38  /**
39   * A {@code TimeUnit} represents time durations at a given unit of
40   * granularity and provides utility methods to convert across units,
41   * and to perform timing and delay operations in these units.  A
42   * {@code TimeUnit} does not maintain time information, but only
43   * helps organize and use time representations that may be maintained
44   * separately across various contexts.  A nanosecond is defined as one
45   * thousandth of a microsecond, a microsecond as one thousandth of a
46   * millisecond, a millisecond as one thousandth of a second, a minute
47   * as sixty seconds, an hour as sixty minutes, and a day as twenty four
48   * hours.
49   *
50   * <p>A {@code TimeUnit} is mainly used to inform time-based methods
51   * how a given timing parameter should be interpreted. For example,
52   * the following code will timeout in 50 milliseconds if the {@link
53   * java.util.concurrent.locks.Lock lock} is not available:
54   *
55   *  <pre> {@code
56   * Lock lock = ...;
57   * if (lock.tryLock(50L, TimeUnit.MILLISECONDS)) ...}</pre>
58   *
59   * while this code will timeout in 50 seconds:
60   *  <pre> {@code
61   * Lock lock = ...;
62   * if (lock.tryLock(50L, TimeUnit.SECONDS)) ...}</pre>
63   *
64   * Note however, that there is no guarantee that a particular timeout
65   * implementation will be able to notice the passage of time at the
66   * same granularity as the given {@code TimeUnit}.
67   *
68   * @since 1.5
69   * @author Doug Lea
70   */
71  public enum TimeUnit {
72      /**
73       * Time unit representing one thousandth of a microsecond
74       */
75      NANOSECONDS {
76          public long toNanos(long d)   { return d; }
77          public long toMicros(long d)  { return d/(C1/C0); }
78          public long toMillis(long d)  { return d/(C2/C0); }
79          public long toSeconds(long d) { return d/(C3/C0); }
80          public long toMinutes(long d) { return d/(C4/C0); }
81          public long toHours(long d)   { return d/(C5/C0); }
82          public long toDays(long d)    { return d/(C6/C0); }
83          public long convert(long d, TimeUnit u) { return u.toNanos(d); }
84          int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
85      },
86  
87      /**
88       * Time unit representing one thousandth of a millisecond
89       */
90      MICROSECONDS {
91          public long toNanos(long d)   { return x(d, C1/C0, MAX/(C1/C0)); }
92          public long toMicros(long d)  { return d; }
93          public long toMillis(long d)  { return d/(C2/C1); }
94          public long toSeconds(long d) { return d/(C3/C1); }
95          public long toMinutes(long d) { return d/(C4/C1); }
96          public long toHours(long d)   { return d/(C5/C1); }
97          public long toDays(long d)    { return d/(C6/C1); }
98          public long convert(long d, TimeUnit u) { return u.toMicros(d); }
99          int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
100     },
101 
102     /**
103      * Time unit representing one thousandth of a second
104      */
105     MILLISECONDS {
106         public long toNanos(long d)   { return x(d, C2/C0, MAX/(C2/C0)); }
107         public long toMicros(long d)  { return x(d, C2/C1, MAX/(C2/C1)); }
108         public long toMillis(long d)  { return d; }
109         public long toSeconds(long d) { return d/(C3/C2); }
110         public long toMinutes(long d) { return d/(C4/C2); }
111         public long toHours(long d)   { return d/(C5/C2); }
112         public long toDays(long d)    { return d/(C6/C2); }
113         public long convert(long d, TimeUnit u) { return u.toMillis(d); }
114         int excessNanos(long d, long m) { return 0; }
115     },
116 
117     /**
118      * Time unit representing one second
119      */
120     SECONDS {
121         public long toNanos(long d)   { return x(d, C3/C0, MAX/(C3/C0)); }
122         public long toMicros(long d)  { return x(d, C3/C1, MAX/(C3/C1)); }
123         public long toMillis(long d)  { return x(d, C3/C2, MAX/(C3/C2)); }
124         public long toSeconds(long d) { return d; }
125         public long toMinutes(long d) { return d/(C4/C3); }
126         public long toHours(long d)   { return d/(C5/C3); }
127         public long toDays(long d)    { return d/(C6/C3); }
128         public long convert(long d, TimeUnit u) { return u.toSeconds(d); }
129         int excessNanos(long d, long m) { return 0; }
130     },
131 
132     /**
133      * Time unit representing sixty seconds
134      */
135     MINUTES {
136         public long toNanos(long d)   { return x(d, C4/C0, MAX/(C4/C0)); }
137         public long toMicros(long d)  { return x(d, C4/C1, MAX/(C4/C1)); }
138         public long toMillis(long d)  { return x(d, C4/C2, MAX/(C4/C2)); }
139         public long toSeconds(long d) { return x(d, C4/C3, MAX/(C4/C3)); }
140         public long toMinutes(long d) { return d; }
141         public long toHours(long d)   { return d/(C5/C4); }
142         public long toDays(long d)    { return d/(C6/C4); }
143         public long convert(long d, TimeUnit u) { return u.toMinutes(d); }
144         int excessNanos(long d, long m) { return 0; }
145     },
146 
147     /**
148      * Time unit representing sixty minutes
149      */
150     HOURS {
151         public long toNanos(long d)   { return x(d, C5/C0, MAX/(C5/C0)); }
152         public long toMicros(long d)  { return x(d, C5/C1, MAX/(C5/C1)); }
153         public long toMillis(long d)  { return x(d, C5/C2, MAX/(C5/C2)); }
154         public long toSeconds(long d) { return x(d, C5/C3, MAX/(C5/C3)); }
155         public long toMinutes(long d) { return x(d, C5/C4, MAX/(C5/C4)); }
156         public long toHours(long d)   { return d; }
157         public long toDays(long d)    { return d/(C6/C5); }
158         public long convert(long d, TimeUnit u) { return u.toHours(d); }
159         int excessNanos(long d, long m) { return 0; }
160     },
161 
162     /**
163      * Time unit representing twenty four hours
164      */
165     DAYS {
166         public long toNanos(long d)   { return x(d, C6/C0, MAX/(C6/C0)); }
167         public long toMicros(long d)  { return x(d, C6/C1, MAX/(C6/C1)); }
168         public long toMillis(long d)  { return x(d, C6/C2, MAX/(C6/C2)); }
169         public long toSeconds(long d) { return x(d, C6/C3, MAX/(C6/C3)); }
170         public long toMinutes(long d) { return x(d, C6/C4, MAX/(C6/C4)); }
171         public long toHours(long d)   { return x(d, C6/C5, MAX/(C6/C5)); }
172         public long toDays(long d)    { return d; }
173         public long convert(long d, TimeUnit u) { return u.toDays(d); }
174         int excessNanos(long d, long m) { return 0; }
175     };
176 
177     // Handy constants for conversion methods
178     static final long C0 = 1L;
179     static final long C1 = C0 * 1000L;
180     static final long C2 = C1 * 1000L;
181     static final long C3 = C2 * 1000L;
182     static final long C4 = C3 * 60L;
183     static final long C5 = C4 * 60L;
184     static final long C6 = C5 * 24L;
185 
186     static final long MAX = Long.MAX_VALUE;
187 
188     /**
189      * Scale d by m, checking for overflow.
190      * This has a short name to make above code more readable.
191      */
192     static long x(long d, long m, long over) {
193         if (d >  over) return Long.MAX_VALUE;
194         if (d < -over) return Long.MIN_VALUE;
195         return d * m;
196     }
197 
198     // To maintain full signature compatibility with 1.5, and to improve the
199     // clarity of the generated javadoc (see 6287639: Abstract methods in
200     // enum classes should not be listed as abstract), method convert
201     // etc. are not declared abstract but otherwise act as abstract methods.
202 
203     /**
204      * Converts the given time duration in the given unit to this unit.
205      * Conversions from finer to coarser granularities truncate, so
206      * lose precision. For example, converting {@code 999} milliseconds
207      * to seconds results in {@code 0}. Conversions from coarser to
208      * finer granularities with arguments that would numerically
209      * overflow saturate to {@code Long.MIN_VALUE} if negative or
210      * {@code Long.MAX_VALUE} if positive.
211      *
212      * <p>For example, to convert 10 minutes to milliseconds, use:
213      * {@code TimeUnit.MILLISECONDS.convert(10L, TimeUnit.MINUTES)}
214      *
215      * @param sourceDuration the time duration in the given {@code sourceUnit}
216      * @param sourceUnit the unit of the {@code sourceDuration} argument
217      * @return the converted duration in this unit,
218      * or {@code Long.MIN_VALUE} if conversion would negatively
219      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
220      */
221     public long convert(long sourceDuration, TimeUnit sourceUnit) {
222         throw new AbstractMethodError();
223     }
224 
225     /**
226      * Equivalent to
227      * {@link #convert(long, TimeUnit) NANOSECONDS.convert(duration, this)}.
228      * @param duration the duration
229      * @return the converted duration,
230      * or {@code Long.MIN_VALUE} if conversion would negatively
231      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
232      */
233     public long toNanos(long duration) {
234         throw new AbstractMethodError();
235     }
236 
237     /**
238      * Equivalent to
239      * {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
240      * @param duration the duration
241      * @return the converted duration,
242      * or {@code Long.MIN_VALUE} if conversion would negatively
243      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
244      */
245     public long toMicros(long duration) {
246         throw new AbstractMethodError();
247     }
248 
249     /**
250      * Equivalent to
251      * {@link #convert(long, TimeUnit) MILLISECONDS.convert(duration, this)}.
252      * @param duration the duration
253      * @return the converted duration,
254      * or {@code Long.MIN_VALUE} if conversion would negatively
255      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
256      */
257     public long toMillis(long duration) {
258         throw new AbstractMethodError();
259     }
260 
261     /**
262      * Equivalent to
263      * {@link #convert(long, TimeUnit) SECONDS.convert(duration, this)}.
264      * @param duration the duration
265      * @return the converted duration,
266      * or {@code Long.MIN_VALUE} if conversion would negatively
267      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
268      */
269     public long toSeconds(long duration) {
270         throw new AbstractMethodError();
271     }
272 
273     /**
274      * Equivalent to
275      * {@link #convert(long, TimeUnit) MINUTES.convert(duration, this)}.
276      * @param duration the duration
277      * @return the converted duration,
278      * or {@code Long.MIN_VALUE} if conversion would negatively
279      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
280      * @since 1.6
281      */
282     public long toMinutes(long duration) {
283         throw new AbstractMethodError();
284     }
285 
286     /**
287      * Equivalent to
288      * {@link #convert(long, TimeUnit) HOURS.convert(duration, this)}.
289      * @param duration the duration
290      * @return the converted duration,
291      * or {@code Long.MIN_VALUE} if conversion would negatively
292      * overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
293      * @since 1.6
294      */
295     public long toHours(long duration) {
296         throw new AbstractMethodError();
297     }
298 
299     /**
300      * Equivalent to
301      * {@link #convert(long, TimeUnit) DAYS.convert(duration, this)}.
302      * @param duration the duration
303      * @return the converted duration
304      * @since 1.6
305      */
306     public long toDays(long duration) {
307         throw new AbstractMethodError();
308     }
309 
310     /**
311      * Utility to compute the excess-nanosecond argument to wait,
312      * sleep, join.
313      * @param d the duration
314      * @param m the number of milliseconds
315      * @return the number of nanoseconds
316      */
317     abstract int excessNanos(long d, long m);
318 
319     /**
320      * Performs a timed {@link Object#wait(long, int) Object.wait}
321      * using this time unit.
322      * This is a convenience method that converts timeout arguments
323      * into the form required by the {@code Object.wait} method.
324      *
325      * <p>For example, you could implement a blocking {@code poll}
326      * method (see {@link BlockingQueue#poll BlockingQueue.poll})
327      * using:
328      *
329      *  <pre> {@code
330      * public synchronized Object poll(long timeout, TimeUnit unit)
331      *     throws InterruptedException {
332      *   while (empty) {
333      *     unit.timedWait(this, timeout);
334      *     ...
335      *   }
336      * }}</pre>
337      *
338      * @param obj the object to wait on
339      * @param timeout the maximum time to wait. If less than
340      * or equal to zero, do not wait at all.
341      * @throws InterruptedException if interrupted while waiting
342      */
343     public void timedWait(Object obj, long timeout)
344             throws InterruptedException {
345         if (timeout > 0) {
346             long ms = toMillis(timeout);
347             int ns = excessNanos(timeout, ms);
348             obj.wait(ms, ns);
349         }
350     }
351 
352     /**
353      * Performs a timed {@link Thread#join(long, int) Thread.join}
354      * using this time unit.
355      * This is a convenience method that converts time arguments into the
356      * form required by the {@code Thread.join} method.
357      *
358      * @param thread the thread to wait for
359      * @param timeout the maximum time to wait. If less than
360      * or equal to zero, do not wait at all.
361      * @throws InterruptedException if interrupted while waiting
362      */
363     public void timedJoin(Thread thread, long timeout)
364             throws InterruptedException {
365         if (timeout > 0) {
366             long ms = toMillis(timeout);
367             int ns = excessNanos(timeout, ms);
368             thread.join(ms, ns);
369         }
370     }
371 
372     /**
373      * Performs a {@link Thread#sleep(long, int) Thread.sleep} using
374      * this time unit.
375      * This is a convenience method that converts time arguments into the
376      * form required by the {@code Thread.sleep} method.
377      *
378      * @param timeout the minimum time to sleep. If less than
379      * or equal to zero, do not sleep at all.
380      * @throws InterruptedException if interrupted while sleeping
381      */
382     public void sleep(long timeout) throws InterruptedException {
383         if (timeout > 0) {
384             long ms = toMillis(timeout);
385             int ns = excessNanos(timeout, ms);
386             Thread.sleep(ms, ns);
387         }
388     }
389 
390 }