1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent; |
8 |
|
9 |
/** |
10 |
* A <tt>TimeUnit</tt> represents time durations at a given unit of |
11 |
* granularity and provides utility methods to convert across units, |
12 |
* and to perform timing and delay operations in these units. |
13 |
* <tt>TimeUnit</tt> is a "featherweight" class. |
14 |
* It does not maintain time information, but only helps organize and |
15 |
* use time representations that may be maintained separately across |
16 |
* various contexts. |
17 |
* A static method {@link #nanoTime} provides access to a high |
18 |
* resolution, nanosecond, timer, which can be used to measure elapsed time. |
19 |
* |
20 |
* <p>The <tt>TimeUnit</tt> class cannot be directly instantiated. |
21 |
* Use the {@link #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, |
22 |
* and {@link #NANOSECONDS} static instances that provide predefined |
23 |
* units of precision. If you use these frequently, consider |
24 |
* statically importing this class. |
25 |
* |
26 |
* <p>A <tt>TimeUnit</tt> is mainly used to inform blocking methods which |
27 |
* can timeout, how the timeout parameter should be interpreted. For example, |
28 |
* the following code will timeout in 50 milliseconds if the {@link Lock lock} |
29 |
* is not available: |
30 |
* <pre> Lock lock = ...; |
31 |
* if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... |
32 |
* </pre> |
33 |
* while this code will timeout in 50 seconds: |
34 |
* <pre> |
35 |
* Lock lock = ...; |
36 |
* if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... |
37 |
* </pre> |
38 |
* Note however, that there is no guarantee that a particular lock, in this |
39 |
* case, will be able to notice the passage of time at the same granularity |
40 |
* as the given <tt>TimeUnit</tt>. |
41 |
* |
42 |
* @since 1.5 |
43 |
* @spec JSR-166 |
44 |
* @revised $Date: 2003/06/09 23:42:27 $ |
45 |
* @editor $Author: dholmes $ |
46 |
* @author Doug Lea |
47 |
*/ |
48 |
public final class TimeUnit implements java.io.Serializable { |
49 |
|
50 |
/** |
51 |
* Return the current value of the system high resolution timer, in |
52 |
* nanoseconds. |
53 |
* <p>This method can only be used to measure elapsed time |
54 |
* and is not related to any notion of system, or wall-clock time. |
55 |
* Although the value returned represents nanoseconds since some |
56 |
* arbitrary start time in the past, the resolution at which this value |
57 |
* is updated is not specified. It provides nanosecond precision, but |
58 |
* not necessarily nanosecond accuracy. |
59 |
* <p>It is guaranteed that successive return |
60 |
* values from this method will not decrease. |
61 |
* |
62 |
* <p> For example, to measure how long some code takes to execute, |
63 |
* with nanosecond precision: |
64 |
* <pre> |
65 |
* long startTime = TimeUnit.nanoTime(); |
66 |
* // ... the code being measured ... |
67 |
* long estimatedTime = TimeUnit.nanoTime() - startTime; |
68 |
* </pre> |
69 |
* |
70 |
* @return The current value of the system high resolution timer, in |
71 |
* nanoseconds. |
72 |
* |
73 |
* @fixme Is this spec tight enough? Too tight? What about issues of |
74 |
* reading the TSC from different processors on a SMP? |
75 |
*/ |
76 |
public static final long nanoTime() { |
77 |
return JSR166Support.currentTimeNanos(); |
78 |
} |
79 |
|
80 |
/** |
81 |
* Convert the given time duration in the given unit to the |
82 |
* current unit. Conversions from finer to coarser granulaties |
83 |
* truncate, so lose precision. Conversions from coarser to finer |
84 |
* granularities may numerically overflow. |
85 |
* |
86 |
* @param duration the time duration in the given <tt>unit</tt> |
87 |
* @param unit the unit of the <tt>duration</tt> argument |
88 |
* @return the converted duration in the current unit. |
89 |
*/ |
90 |
public long convert(long duration, TimeUnit unit) { |
91 |
if (unit == this) |
92 |
return duration; |
93 |
if (index > unit.index) |
94 |
return duration / multipliers[index - unit.index]; |
95 |
else |
96 |
return duration * multipliers[unit.index - index]; |
97 |
} |
98 |
|
99 |
/** |
100 |
* Equivalent to <code>NANOSECONDS.convert(duration, this)</code>. |
101 |
* @param duration the duration |
102 |
* @return the converted duration. |
103 |
**/ |
104 |
public long toNanos(long duration) { |
105 |
if (index == NS) |
106 |
return duration; |
107 |
else |
108 |
return duration * multipliers[index]; |
109 |
} |
110 |
|
111 |
/** |
112 |
* Perform a timed <tt>Object.wait</tt> using the current time unit. |
113 |
* This is a convenience method that converts timeout arguments into the |
114 |
* form required by the <tt>Object.wait</tt> method. |
115 |
* <p>For example, you could implement a blocking <tt>poll</tt> method (see |
116 |
* {@link BlockingQueue#poll BlockingQueue.poll} using: |
117 |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
118 |
* while (empty) { |
119 |
* unit.timedWait(this, timeout); |
120 |
* ... |
121 |
* } |
122 |
* }</pre> |
123 |
* @param obj the object to wait on |
124 |
* @param timeout the maximum time to wait |
125 |
* @throws InterruptedException if interrupted while waiting. |
126 |
* @see Object#wait(long, int) |
127 |
*/ |
128 |
public void timedWait(Object obj, long timeout) |
129 |
throws InterruptedException { |
130 |
long ms = MILLISECONDS.convert(timeout, this); |
131 |
int ns = excessNanos(timeout, ms); |
132 |
obj.wait(ms, ns); |
133 |
} |
134 |
|
135 |
/** |
136 |
* Perform a timed <tt>Thread.join</tt> using the current time unit. |
137 |
* This is a convenience method that converts time arguments into the |
138 |
* form required by the <tt>Thread.join</tt> method. |
139 |
* @param thread the thread to wait for |
140 |
* @param timeout the maximum time to wait |
141 |
* @throws InterruptedException if interrupted while waiting. |
142 |
* @see Thread#join(long, int) |
143 |
*/ |
144 |
public void timedJoin(Thread thread, long timeout) |
145 |
throws InterruptedException { |
146 |
long ms = MILLISECONDS.convert(timeout, this); |
147 |
int ns = excessNanos(timeout, ms); |
148 |
thread.join(ms, ns); |
149 |
} |
150 |
|
151 |
/** |
152 |
* Perform a <tt>Thread.sleep</tt> using the current time unit. |
153 |
* This is a convenience method that converts time arguments into the |
154 |
* form required by the <tt>Thread.sleep</tt> method. |
155 |
* @param timeout the minimum time to sleep |
156 |
* @throws InterruptedException if interrupted while sleeping. |
157 |
* @see Thread#sleep |
158 |
*/ |
159 |
public void sleep(long timeout) throws InterruptedException { |
160 |
long ms = MILLISECONDS.convert(timeout, this); |
161 |
int ns = excessNanos(timeout, ms); |
162 |
Thread.sleep(ms, ns); |
163 |
} |
164 |
|
165 |
/* ordered indices for each time unit */ |
166 |
private static final int NS = 0; |
167 |
private static final int US = 1; |
168 |
private static final int MS = 2; |
169 |
private static final int S = 3; |
170 |
|
171 |
/** quick lookup table for conversion factors */ |
172 |
static final int[] multipliers = { 1, 1000, 1000*1000, 1000*1000*1000 }; |
173 |
|
174 |
/** the index of this unit */ |
175 |
int index; |
176 |
|
177 |
/** private constructor */ |
178 |
TimeUnit(int index) { this.index = index; } |
179 |
|
180 |
/** |
181 |
* Utility method to compute the excess-nanosecond argument to |
182 |
* wait, sleep, join. |
183 |
* @fixme overflow? |
184 |
*/ |
185 |
private int excessNanos(long time, long ms) { |
186 |
if (index == NS) |
187 |
return (int) (time - (ms * multipliers[MS-NS])); |
188 |
else if (index == US) |
189 |
return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS])); |
190 |
else |
191 |
return 0; |
192 |
} |
193 |
|
194 |
/** Unit for one-second granularities */ |
195 |
public static final TimeUnit SECONDS = new TimeUnit(S); |
196 |
|
197 |
/** Unit for one-millisecond granularities */ |
198 |
public static final TimeUnit MILLISECONDS = new TimeUnit(MS); |
199 |
|
200 |
/** Unit for one-microsecond granularities */ |
201 |
public static final TimeUnit MICROSECONDS = new TimeUnit(US); |
202 |
|
203 |
/** Unit for one-nanosecond granularities */ |
204 |
public static final TimeUnit NANOSECONDS = new TimeUnit(NS); |
205 |
|
206 |
} |