ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.20
Committed: Tue Jan 13 12:59:26 2004 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.19: +47 -85 lines
Log Message:
TimeUnit now an enum

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.18 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 dl 1.2 */
6    
7 tim 1.1 package java.util.concurrent;
8    
9     /**
10 tim 1.6 * A <tt>TimeUnit</tt> represents time durations at a given unit of
11     * granularity and provides utility methods to convert across units,
12 dl 1.20 * and to perform timing and delay operations in these units. A
13     * <tt>TimeUnit</tt> does not maintain time information, but only
14     * helps organize and use time representations that may be maintained
15     * separately across various contexts.
16 tim 1.1 *
17 dl 1.20 * <p>A <tt>TimeUnit</tt> is mainly used to inform time-based methods
18     * how a given timing parameter should be interpreted. For example,
19     * the following code will timeout in 50 milliseconds if the {@link
20     * java.util.concurrent.locks.Lock lock} is not available:
21 dl 1.19 *
22 tim 1.1 * <pre> Lock lock = ...;
23     * if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ...
24     * </pre>
25     * while this code will timeout in 50 seconds:
26 tim 1.6 * <pre>
27 tim 1.1 * Lock lock = ...;
28     * if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ...
29     * </pre>
30 dl 1.15 *
31     * Note however, that there is no guarantee that a particular timeout
32     * implementation will be able to notice the passage of time at the
33     * same granularity as the given <tt>TimeUnit</tt>.
34 tim 1.1 *
35     * @since 1.5
36 dl 1.4 * @author Doug Lea
37 tim 1.1 */
38 dl 1.20 public enum TimeUnit {
39     NANOSECONDS(0), MICROSECONDS(1), MILLISECONDS(2), SECONDS(3);
40 dl 1.14
41     /** the index of this unit */
42 dl 1.20 private final int index;
43 dl 1.14
44 dl 1.20 /** Internal constructor */
45     TimeUnit(int index) {
46 dl 1.14 this.index = index;
47     }
48    
49 dl 1.20 /** Lookup table for conversion factors */
50     private static final int[] multipliers = {
51     1,
52     1000,
53     1000 * 1000,
54     1000 * 1000 * 1000
55     };
56    
57     /**
58     * Lookup table to check saturation. Note that because we are
59     * dividing these down, we don't have to deal with asymmetry of
60     * MIN/MAX values.
61 dl 1.14 */
62 dl 1.20 private static final long[] overflows = {
63     0, // unused
64     Long.MAX_VALUE / 1000,
65     Long.MAX_VALUE / (1000 * 1000),
66     Long.MAX_VALUE / (1000 * 1000 * 1000)
67     };
68 tim 1.1
69     /**
70 dl 1.14 * Perform conversion based on given delta representing the
71 dl 1.13 * difference between units
72     * @param delta the difference in index values of source and target units
73     * @param duration the duration
74     * @return converted duration or saturated value
75     */
76     private static long doConvert(int delta, long duration) {
77     if (delta == 0)
78     return duration;
79     if (delta < 0)
80     return duration / multipliers[-delta];
81     if (duration > overflows[delta])
82     return Long.MAX_VALUE;
83     if (duration < -overflows[delta])
84     return Long.MIN_VALUE;
85     return duration * multipliers[delta];
86     }
87    
88     /**
89 dl 1.17 * Convert the given time duration in the given unit to this
90     * unit. Conversions from finer to coarser granularities
91 dl 1.13 * truncate, so lose precision. For example converting
92     * <tt>999</tt> milliseconds to seconds results in
93     * <tt>0</tt>. Conversions from coarser to finer granularities
94     * with arguments that would numerically overflow saturate to
95     * <tt>Long.MIN_VALUE</tt> if negative or <tt>Long.MAX_VALUE</tt>
96     * if positive.
97 tim 1.1 *
98     * @param duration the time duration in the given <tt>unit</tt>
99     * @param unit the unit of the <tt>duration</tt> argument
100 dl 1.17 * @return the converted duration in this unit,
101 dl 1.10 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
102 dl 1.11 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
103 tim 1.1 */
104     public long convert(long duration, TimeUnit unit) {
105 dl 1.13 return doConvert(unit.index - index, duration);
106 dl 1.2 }
107    
108     /**
109 dl 1.12 * Equivalent to <tt>NANOSECONDS.convert(duration, this)</tt>.
110 dl 1.2 * @param duration the duration
111 dl 1.13 * @return the converted duration,
112 dl 1.10 * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
113 dl 1.11 * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
114 dl 1.13 * @see #convert
115     */
116 dl 1.2 public long toNanos(long duration) {
117 dl 1.13 return doConvert(index, duration);
118     }
119    
120     /**
121     * Equivalent to <tt>MICROSECONDS.convert(duration, this)</tt>.
122     * @param duration the duration
123     * @return the converted duration,
124     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
125     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
126     * @see #convert
127     */
128     public long toMicros(long duration) {
129 dl 1.20 return doConvert(index - MICROSECONDS.index, duration);
130 dl 1.13 }
131    
132     /**
133     * Equivalent to <tt>MILLISECONDS.convert(duration, this)</tt>.
134     * @param duration the duration
135     * @return the converted duration,
136     * or <tt>Long.MIN_VALUE</tt> if conversion would negatively
137     * overflow, or <tt>Long.MAX_VALUE</tt> if it would positively overflow.
138     * @see #convert
139     */
140     public long toMillis(long duration) {
141 dl 1.20 return doConvert(index - MILLISECONDS.index, duration);
142 dl 1.13 }
143    
144     /**
145     * Equivalent to <tt>SECONDS.convert(duration, this)</tt>.
146     * @param duration the duration
147     * @return the converted duration.
148     * @see #convert
149     */
150     public long toSeconds(long duration) {
151 dl 1.20 return doConvert(index - SECONDS.index, duration);
152     }
153    
154    
155     /**
156     * Utility method to compute the excess-nanosecond argument to
157     * wait, sleep, join.
158     */
159     private int excessNanos(long time, long ms) {
160     if (this == NANOSECONDS)
161     return (int) (time - (ms * 1000 * 1000));
162     if (this == MICROSECONDS)
163     return (int) ((time * 1000) - (ms * 1000 * 1000));
164     return 0;
165 tim 1.1 }
166 tim 1.6
167 tim 1.1 /**
168 dl 1.17 * Perform a timed <tt>Object.wait</tt> using this time unit.
169 dl 1.19 * This is a convenience method that converts timeout arguments
170     * into the form required by the <tt>Object.wait</tt> method.
171     *
172     * <p>For example, you could implement a blocking <tt>poll</tt>
173     * method (see {@link BlockingQueue#poll BlockingQueue.poll}
174     * using:
175     *
176 tim 1.1 * <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException {
177     * while (empty) {
178     * unit.timedWait(this, timeout);
179     * ...
180     * }
181     * }</pre>
182 dl 1.19 *
183 tim 1.1 * @param obj the object to wait on
184 dl 1.10 * @param timeout the maximum time to wait.
185 tim 1.1 * @throws InterruptedException if interrupted while waiting.
186     * @see Object#wait(long, int)
187     */
188     public void timedWait(Object obj, long timeout)
189     throws InterruptedException {
190 dl 1.10 if (timeout > 0) {
191 dl 1.15 long ms = toMillis(timeout);
192 dl 1.10 int ns = excessNanos(timeout, ms);
193     obj.wait(ms, ns);
194     }
195 tim 1.1 }
196    
197     /**
198 dl 1.17 * Perform a timed <tt>Thread.join</tt> using this time unit.
199 tim 1.1 * This is a convenience method that converts time arguments into the
200     * form required by the <tt>Thread.join</tt> method.
201     * @param thread the thread to wait for
202     * @param timeout the maximum time to wait
203     * @throws InterruptedException if interrupted while waiting.
204     * @see Thread#join(long, int)
205     */
206     public void timedJoin(Thread thread, long timeout)
207     throws InterruptedException {
208 dl 1.10 if (timeout > 0) {
209 dl 1.15 long ms = toMillis(timeout);
210 dl 1.10 int ns = excessNanos(timeout, ms);
211     thread.join(ms, ns);
212     }
213 tim 1.1 }
214 tim 1.6
215 tim 1.1 /**
216 dl 1.17 * Perform a <tt>Thread.sleep</tt> using this unit.
217 tim 1.1 * This is a convenience method that converts time arguments into the
218     * form required by the <tt>Thread.sleep</tt> method.
219 tim 1.6 * @param timeout the minimum time to sleep
220 tim 1.1 * @throws InterruptedException if interrupted while sleeping.
221     * @see Thread#sleep
222     */
223     public void sleep(long timeout) throws InterruptedException {
224 dl 1.10 if (timeout > 0) {
225 dl 1.15 long ms = toMillis(timeout);
226 dl 1.10 int ns = excessNanos(timeout, ms);
227     Thread.sleep(ms, ns);
228     }
229 tim 1.1 }
230    
231     }