ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/TimeUnit.java
Revision: 1.8
Committed: Wed Aug 6 18:22:09 2003 UTC (20 years, 10 months ago) by tim
Branch: MAIN
CVS Tags: JSR166_CR1
Changes since 1.7: +6 -6 lines
Log Message:
Fixes to minor errors found by DocCheck

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 tim 1.8 * @revised $Date: 2003/08/06 16:08: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 tim 1.6 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     /* 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 tim 1.6 /**
149 tim 1.1 * Utility method to compute the excess-nanosecond argument to
150 dl 1.7 * wait, sleep, join. The results may overflow, so public methods
151     * invoking this should document possible overflow unless
152     * overflow is known not to be possible for the given arguments.
153 tim 1.1 */
154     private int excessNanos(long time, long ms) {
155 tim 1.6 if (index == NS)
156 tim 1.1 return (int) (time - (ms * multipliers[MS-NS]));
157 tim 1.6 else if (index == US)
158 tim 1.1 return (int) ((time * multipliers[US-NS]) - (ms * multipliers[MS-NS]));
159 tim 1.6 else
160 tim 1.1 return 0;
161     }
162    
163 tim 1.8 /** Unit for one-second granularities. */
164 tim 1.1 public static final TimeUnit SECONDS = new TimeUnit(S);
165    
166 tim 1.8 /** Unit for one-millisecond granularities. */
167 tim 1.1 public static final TimeUnit MILLISECONDS = new TimeUnit(MS);
168    
169 tim 1.8 /** Unit for one-microsecond granularities. */
170 tim 1.1 public static final TimeUnit MICROSECONDS = new TimeUnit(US);
171    
172 tim 1.8 /** Unit for one-nanosecond granularities. */
173 tim 1.1 public static final TimeUnit NANOSECONDS = new TimeUnit(NS);
174    
175     }