ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.9
Committed: Thu Aug 7 16:00:28 2003 UTC (20 years, 10 months ago) by dl
Branch: MAIN
Changes since 1.8: +20 -8 lines
Log Message:
ScheduledExecutor must prestart core threads

File Contents

# User Rev Content
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 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     * and to perform timing and delay operations in these units.
13     * <tt>TimeUnit</tt> is a &quot;featherweight&quot; class.
14     * It does not maintain time information, but only helps organize and
15     * use time representations that may be maintained separately across
16 tim 1.1 * various contexts.
17     *
18 tim 1.6 * <p>The <tt>TimeUnit</tt> class cannot be directly instantiated.
19 tim 1.1 * 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 tim 1.6 * the following code will timeout in 50 milliseconds if the {@link java.util.concurrent.locks.Lock lock}
27 tim 1.1 * 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 tim 1.6 * <pre>
33 tim 1.1 * 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.9 * @revised $Date: 2003/08/06 18:22:09 $
43     * @editor $Author: tim $
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 dl 1.9 else if (index > unit.index)
62 tim 1.1 return duration / multipliers[index - unit.index];
63 tim 1.6 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 tim 1.6
79 tim 1.1 /**
80 tim 1.6 * Perform a timed <tt>Object.wait</tt> using the current time unit.
81 tim 1.1 * This is a convenience method that converts timeout arguments into the
82 dl 1.7 * form required by the <tt>Object.wait</tt> method.
83 tim 1.1 * <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 tim 1.6
119 tim 1.1 /**
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 tim 1.6 * @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 dl 1.9 /**
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 tim 1.1 /* 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 dl 1.4 /** quick lookup table for conversion factors */
149 tim 1.1 static final int[] multipliers = { 1, 1000, 1000*1000, 1000*1000*1000 };
150    
151 dl 1.4 /** the index of this unit */
152 tim 1.1 int index;
153    
154     /** private constructor */
155 dl 1.9 TimeUnit(int index, String name) {
156     this.index = index;
157     this.unitName = name;
158     }
159 tim 1.1
160 tim 1.6 /**
161 tim 1.1 * Utility method to compute the excess-nanosecond argument to
162 dl 1.7 * 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 tim 1.1 */
166     private int excessNanos(long time, long ms) {
167 tim 1.6 if (index == NS)
168 tim 1.1 return (int) (time - (ms * multipliers[MS-NS]));
169 tim 1.6 else if (index == US)
170 tim 1.1 return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS]));
171 tim 1.6 else
172 tim 1.1 return 0;
173     }
174    
175 tim 1.8 /** Unit for one-second granularities. */
176 dl 1.9 public static final TimeUnit SECONDS = new TimeUnit(S, "seconds");
177 tim 1.1
178 tim 1.8 /** Unit for one-millisecond granularities. */
179 dl 1.9 public static final TimeUnit MILLISECONDS = new TimeUnit(MS, "milliseconds");
180 tim 1.1
181 tim 1.8 /** Unit for one-microsecond granularities. */
182 dl 1.9 public static final TimeUnit MICROSECONDS = new TimeUnit(US, "microseconds");
183 tim 1.1
184 tim 1.8 /** Unit for one-nanosecond granularities. */
185 dl 1.9 public static final TimeUnit NANOSECONDS = new TimeUnit(NS, "nanoseconds");
186 tim 1.1
187     }