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 |
|
/** |
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 #highResolutionTime} provides access to a high |
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. |
56 |
|
* and is not related to any notion of system, or wall-clock time. |
57 |
|
* Although the value returned represents nanoseconds since some |
58 |
|
* arbitrary start time in the past, the resolution at which this value |
59 |
< |
* is updated is not specified. So we have nanosecond precision, but |
59 |
> |
* is updated is not specified. It provides have nanosecond precision, but |
60 |
|
* not necessarily nanosecond accuracy. |
61 |
|
* It is guaranteed that successive return |
62 |
|
* values from this method will not decrease. |
64 |
|
* <p> For example to measure how long some code takes to execute, |
65 |
|
* with nanosecond precision: |
66 |
|
* <pre> |
67 |
< |
* long startTime = TimeUnit.highResolutionTime(); |
67 |
> |
* long startTime = TimeUnit.nanoTime(); |
68 |
|
* // ... the code being measured ... |
69 |
< |
* long estimatedTime = TimeUnit.highResolutionTime() - startTime; |
69 |
> |
* long estimatedTime = TimeUnit.nanoTime() - startTime; |
70 |
|
* </pre> |
71 |
|
* |
72 |
|
* @return The current value of the system high resolution timer, in |
75 |
|
* @fixme Is this spec tight enough? Too tight? What about issues of |
76 |
|
* reading the TSC from different processors on a SMP? |
77 |
|
*/ |
78 |
< |
public static final long highResolutionTime() { |
79 |
< |
return systemTimeNanos(); |
78 |
> |
public static final long nanoTime() { |
79 |
> |
return JSR166Support.currentTimeNanos(); |
80 |
|
} |
81 |
|
|
82 |
|
/** |
90 |
|
* @return the converted duration in the current unit. |
91 |
|
*/ |
92 |
|
public long convert(long duration, TimeUnit unit) { |
93 |
< |
if (index > unit.index) { |
93 |
> |
if (unit == this) |
94 |
> |
return duration; |
95 |
> |
if (index > unit.index) |
96 |
|
return duration / multipliers[index - unit.index]; |
97 |
< |
} |
90 |
< |
else { |
97 |
> |
else |
98 |
|
return duration * multipliers[unit.index - index]; |
99 |
< |
} |
99 |
> |
} |
100 |
> |
|
101 |
> |
/** |
102 |
> |
* Equivalent to <code>NANOOSECONDS.convert(duration, this)</code>. |
103 |
> |
* @param duration the duration |
104 |
> |
* @return the converted duration. |
105 |
> |
**/ |
106 |
> |
public long toNanos(long duration) { |
107 |
> |
if (index == NS) |
108 |
> |
return duration; |
109 |
> |
else |
110 |
> |
return duration * multipliers[index]; |
111 |
|
} |
112 |
|
|
113 |
|
/** |
193 |
|
return 0; |
194 |
|
} |
195 |
|
|
178 |
– |
/** Underlying native time call */ |
179 |
– |
static native long systemTimeNanos(); |
180 |
– |
|
196 |
|
/** Unit for one-second granularities */ |
197 |
|
public static final TimeUnit SECONDS = new TimeUnit(S); |
198 |
|
|