1 |
dl |
1.2 |
/* |
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 |
tim |
1.1 |
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 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 |
dl |
1.5 |
* @revised $Date: 2003/06/24 14:34:49 $ |
43 |
|
|
* @editor $Author: dl $ |
44 |
dl |
1.4 |
* @author Doug Lea |
45 |
tim |
1.1 |
*/ |
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 |
dl |
1.2 |
if (unit == this) |
60 |
|
|
return duration; |
61 |
|
|
if (index > unit.index) |
62 |
tim |
1.1 |
return duration / multipliers[index - unit.index]; |
63 |
dl |
1.2 |
else |
64 |
tim |
1.1 |
return duration * multipliers[unit.index - index]; |
65 |
dl |
1.2 |
} |
66 |
|
|
|
67 |
|
|
/** |
68 |
dholmes |
1.3 |
* Equivalent to <code>NANOSECONDS.convert(duration, this)</code>. |
69 |
dl |
1.2 |
* @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 |
tim |
1.1 |
} |
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 |
dl |
1.4 |
* @param timeout the minimum time to sleep |
124 |
tim |
1.1 |
* @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 |
|
|
/* ordered indices for each time unit */ |
134 |
|
|
private static final int NS = 0; |
135 |
|
|
private static final int US = 1; |
136 |
|
|
private static final int MS = 2; |
137 |
|
|
private static final int S = 3; |
138 |
|
|
|
139 |
dl |
1.4 |
/** quick lookup table for conversion factors */ |
140 |
tim |
1.1 |
static final int[] multipliers = { 1, 1000, 1000*1000, 1000*1000*1000 }; |
141 |
|
|
|
142 |
dl |
1.4 |
/** the index of this unit */ |
143 |
tim |
1.1 |
int index; |
144 |
|
|
|
145 |
|
|
/** private constructor */ |
146 |
|
|
TimeUnit(int index) { this.index = index; } |
147 |
|
|
|
148 |
|
|
/** |
149 |
|
|
* Utility method to compute the excess-nanosecond argument to |
150 |
|
|
* wait, sleep, join. |
151 |
|
|
* @fixme overflow? |
152 |
|
|
*/ |
153 |
|
|
private int excessNanos(long time, long ms) { |
154 |
|
|
if (index == NS) |
155 |
|
|
return (int) (time - (ms * multipliers[MS-NS])); |
156 |
|
|
else if (index == US) |
157 |
|
|
return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS])); |
158 |
|
|
else |
159 |
|
|
return 0; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
/** Unit for one-second granularities */ |
163 |
|
|
public static final TimeUnit SECONDS = new TimeUnit(S); |
164 |
|
|
|
165 |
|
|
/** Unit for one-millisecond granularities */ |
166 |
|
|
public static final TimeUnit MILLISECONDS = new TimeUnit(MS); |
167 |
|
|
|
168 |
|
|
/** Unit for one-microsecond granularities */ |
169 |
|
|
public static final TimeUnit MICROSECONDS = new TimeUnit(US); |
170 |
|
|
|
171 |
|
|
/** Unit for one-nanosecond granularities */ |
172 |
|
|
public static final TimeUnit NANOSECONDS = new TimeUnit(NS); |
173 |
|
|
|
174 |
|
|
} |