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 |
* |
18 |
* <p>The <tt>TimeUnit</tt> class cannot be directly instantiated. |
19 |
* Use the {@link #SECONDS}, {@link #MILLISECONDS}, {@link #MICROSECONDS}, |
20 |
* and {@link #NANOSECONDS} static instances that provide predefined |
21 |
* units of precision. If you use these frequently, consider |
22 |
* statically importing this class. |
23 |
* |
24 |
* <p>A <tt>TimeUnit</tt> is mainly used to inform blocking methods which |
25 |
* can timeout, how the timeout parameter should be interpreted. For example, |
26 |
* the following code will timeout in 50 milliseconds if the {@link java.util.concurrent.locks.Lock lock} |
27 |
* is not available: |
28 |
* <pre> Lock lock = ...; |
29 |
* if ( lock.tryLock(50L, TimeUnit.MILLISECONDS) ) ... |
30 |
* </pre> |
31 |
* while this code will timeout in 50 seconds: |
32 |
* <pre> |
33 |
* Lock lock = ...; |
34 |
* if ( lock.tryLock(50L, TimeUnit.SECONDS) ) ... |
35 |
* </pre> |
36 |
* Note however, that there is no guarantee that a particular lock, in this |
37 |
* case, will be able to notice the passage of time at the same granularity |
38 |
* as the given <tt>TimeUnit</tt>. |
39 |
* |
40 |
* @since 1.5 |
41 |
* @spec JSR-166 |
42 |
* @revised $Date: 2003/08/06 18:22:09 $ |
43 |
* @editor $Author: tim $ |
44 |
* @author Doug Lea |
45 |
*/ |
46 |
public final class TimeUnit implements java.io.Serializable { |
47 |
|
48 |
/** |
49 |
* Convert the given time duration in the given unit to the |
50 |
* current unit. Conversions from finer to coarser granulaties |
51 |
* truncate, so lose precision. Conversions from coarser to finer |
52 |
* granularities may numerically overflow. |
53 |
* |
54 |
* @param duration the time duration in the given <tt>unit</tt> |
55 |
* @param unit the unit of the <tt>duration</tt> argument |
56 |
* @return the converted duration in the current unit. |
57 |
*/ |
58 |
public long convert(long duration, TimeUnit unit) { |
59 |
if (unit == this) |
60 |
return duration; |
61 |
else if (index > unit.index) |
62 |
return duration / multipliers[index - unit.index]; |
63 |
else |
64 |
return duration * multipliers[unit.index - index]; |
65 |
} |
66 |
|
67 |
/** |
68 |
* Equivalent to <code>NANOSECONDS.convert(duration, this)</code>. |
69 |
* @param duration the duration |
70 |
* @return the converted duration. |
71 |
**/ |
72 |
public long toNanos(long duration) { |
73 |
if (index == NS) |
74 |
return duration; |
75 |
else |
76 |
return duration * multipliers[index]; |
77 |
} |
78 |
|
79 |
/** |
80 |
* Perform a timed <tt>Object.wait</tt> using the current time unit. |
81 |
* This is a convenience method that converts timeout arguments into the |
82 |
* form required by the <tt>Object.wait</tt> method. |
83 |
* <p>For example, you could implement a blocking <tt>poll</tt> method (see |
84 |
* {@link BlockingQueue#poll BlockingQueue.poll} using: |
85 |
* <pre> public synchronized Object poll(long timeout, TimeUnit unit) throws InterruptedException { |
86 |
* while (empty) { |
87 |
* unit.timedWait(this, timeout); |
88 |
* ... |
89 |
* } |
90 |
* }</pre> |
91 |
* @param obj the object to wait on |
92 |
* @param timeout the maximum time to wait |
93 |
* @throws InterruptedException if interrupted while waiting. |
94 |
* @see Object#wait(long, int) |
95 |
*/ |
96 |
public void timedWait(Object obj, long timeout) |
97 |
throws InterruptedException { |
98 |
long ms = MILLISECONDS.convert(timeout, this); |
99 |
int ns = excessNanos(timeout, ms); |
100 |
obj.wait(ms, ns); |
101 |
} |
102 |
|
103 |
/** |
104 |
* Perform a timed <tt>Thread.join</tt> using the current time unit. |
105 |
* This is a convenience method that converts time arguments into the |
106 |
* form required by the <tt>Thread.join</tt> method. |
107 |
* @param thread the thread to wait for |
108 |
* @param timeout the maximum time to wait |
109 |
* @throws InterruptedException if interrupted while waiting. |
110 |
* @see Thread#join(long, int) |
111 |
*/ |
112 |
public void timedJoin(Thread thread, long timeout) |
113 |
throws InterruptedException { |
114 |
long ms = MILLISECONDS.convert(timeout, this); |
115 |
int ns = excessNanos(timeout, ms); |
116 |
thread.join(ms, ns); |
117 |
} |
118 |
|
119 |
/** |
120 |
* Perform a <tt>Thread.sleep</tt> using the current time unit. |
121 |
* This is a convenience method that converts time arguments into the |
122 |
* form required by the <tt>Thread.sleep</tt> method. |
123 |
* @param timeout the minimum time to sleep |
124 |
* @throws InterruptedException if interrupted while sleeping. |
125 |
* @see Thread#sleep |
126 |
*/ |
127 |
public void sleep(long timeout) throws InterruptedException { |
128 |
long ms = MILLISECONDS.convert(timeout, this); |
129 |
int ns = excessNanos(timeout, ms); |
130 |
Thread.sleep(ms, ns); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Return the common name for this unit. |
135 |
*/ |
136 |
public String toString() { |
137 |
return unitName; |
138 |
} |
139 |
|
140 |
private final String unitName; |
141 |
|
142 |
/* ordered indices for each time unit */ |
143 |
private static final int NS = 0; |
144 |
private static final int US = 1; |
145 |
private static final int MS = 2; |
146 |
private static final int S = 3; |
147 |
|
148 |
/** quick lookup table for conversion factors */ |
149 |
static final int[] multipliers = { 1, 1000, 1000*1000, 1000*1000*1000 }; |
150 |
|
151 |
/** the index of this unit */ |
152 |
int index; |
153 |
|
154 |
/** private constructor */ |
155 |
TimeUnit(int index, String name) { |
156 |
this.index = index; |
157 |
this.unitName = name; |
158 |
} |
159 |
|
160 |
/** |
161 |
* Utility method to compute the excess-nanosecond argument to |
162 |
* wait, sleep, join. The results may overflow, so public methods |
163 |
* invoking this should document possible overflow unless |
164 |
* overflow is known not to be possible for the given arguments. |
165 |
*/ |
166 |
private int excessNanos(long time, long ms) { |
167 |
if (index == NS) |
168 |
return (int) (time - (ms * multipliers[MS-NS])); |
169 |
else if (index == US) |
170 |
return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS])); |
171 |
else |
172 |
return 0; |
173 |
} |
174 |
|
175 |
/** Unit for one-second granularities. */ |
176 |
public static final TimeUnit SECONDS = new TimeUnit(S, "seconds"); |
177 |
|
178 |
/** Unit for one-millisecond granularities. */ |
179 |
public static final TimeUnit MILLISECONDS = new TimeUnit(MS, "milliseconds"); |
180 |
|
181 |
/** Unit for one-microsecond granularities. */ |
182 |
public static final TimeUnit MICROSECONDS = new TimeUnit(US, "microseconds"); |
183 |
|
184 |
/** Unit for one-nanosecond granularities. */ |
185 |
public static final TimeUnit NANOSECONDS = new TimeUnit(NS, "nanoseconds"); |
186 |
|
187 |
} |