public class ElapsedTime
extends java.lang.Object
ElapsedTime
class provides a simple handy timer to measure elapsed time intervals.
The timer does not provide events or callbacks, as some other timers do. Rather, at an application-
determined juncture, one can reset()
the timer. Thereafter, one can query the interval
of wall-clock time that has subsequently elapsed by calling the time()
, seconds()
,
or milliseconds()
methods. The timer has nanosecond internal accuracy. The precision
reported by the time()
method is either seconds or milliseconds, depending on how the
timer is initially constructed.
This class is thread-safe.Modifier and Type | Class and Description |
---|---|
static class |
ElapsedTime.Resolution
An indicator of the resolution of a timer.
|
Modifier and Type | Field and Description |
---|---|
static long |
MILLIS_IN_NANO
the number of nanoseconds in a millisecond
|
protected long |
nsStartTime |
protected double |
resolution |
static long |
SECOND_IN_NANO
the number of nanoseconds in a second
|
Constructor and Description |
---|
ElapsedTime()
Creates a timer with resolution
Resolution.Seconds
that is initialized with the now-current time. |
ElapsedTime(ElapsedTime.Resolution resolution)
Creates a timer with a resolution of seconds or milliseconds.
|
ElapsedTime(long startTime)
Creates a timer with resolution
Resolution.Seconds . |
Modifier and Type | Method and Description |
---|---|
ElapsedTime.Resolution |
getResolution()
Returns the resolution with which the timer was instantiated.
|
void |
log(java.lang.String label)
Log a message stating how long the timer has been running
|
double |
milliseconds()
Returns the duration that has elapsed since the last reset of this timer in milliseconds
|
long |
nanoseconds()
Returns the duration that has elapsed since the last reset of this timer in nanoseconds
|
long |
now(java.util.concurrent.TimeUnit unit)
Returns the current time on the clock used by the timer
|
protected long |
nsNow() |
void |
reset()
Resets the internal state of the timer to reflect the current time.
|
double |
seconds()
Returns the duration that has elapsed since the last reset of this timer in seconds
|
double |
startTime()
Returns, in resolution-dependent units, the time at which this timer was last reset.
|
long |
startTimeNanoseconds()
Returns the time at which the timer was last reset, in units of nanoseconds
|
double |
time()
Returns the duration that has elapsed since the last reset of this timer.
|
long |
time(java.util.concurrent.TimeUnit unit)
Returns the duration that has elapsed since the last reset of this timer
as an integer in the units requested.
|
java.lang.String |
toString()
Returns a string indicating the current elapsed time of the timer.
|
public static final long SECOND_IN_NANO
public static final long MILLIS_IN_NANO
protected volatile long nsStartTime
protected final double resolution
public ElapsedTime()
Resolution.Seconds
that is initialized with the now-current time.ElapsedTime(long)
,
ElapsedTime(Resolution)
public ElapsedTime(long startTime)
Resolution.Seconds
.
The timer is initialized with the provided start time. Zero is often a useful value to provide
here: in common usage such timers will often be processed by application logic virtually immediately.startTime
- the initial value of the timerElapsedTime()
public ElapsedTime(ElapsedTime.Resolution resolution)
time()
method reports. The timer is initialized
with the current time.resolution
- the resolution of the new timerElapsedTime()
protected long nsNow()
public long now(java.util.concurrent.TimeUnit unit)
unit
- the time unit in which the current time should be returnedpublic void reset()
time()
will report as zero.time()
public double startTime()
public long startTimeNanoseconds()
public double time()
ElapsedTime()
,
ElapsedTime(Resolution)
,
seconds()
,
milliseconds()
public long time(java.util.concurrent.TimeUnit unit)
unit
- the units in which to return the answerpublic double seconds()
time()
public double milliseconds()
time()
public long nanoseconds()
time()
public ElapsedTime.Resolution getResolution()
public void log(java.lang.String label)
public java.lang.String toString()
toString
in class java.lang.Object