Package io.microsphere.util
Class StopWatch.Task
- java.lang.Object
-
- io.microsphere.util.StopWatch.Task
-
- Enclosing class:
- StopWatch
public static class StopWatch.Task extends java.lang.ObjectRepresents a named task tracked by aStopWatch. Each task records its start time in nanoseconds and calculates the elapsed time whenstop()is called.Tasks are compared by their
taskNameonly, making task names unique within a single stop watch's running task list.Example Usage
StopWatch.Task task = StopWatch.Task.start("parsing"); // perform parsing work task.stop(); System.out.println(task.getTaskName()); // "parsing" System.out.println(task.getElapsedNanos()); // elapsed time in nanoseconds- Since:
- 1.0.0
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object o)Compares this task with the specified object for equality.longgetElapsedNanos()Returns the elapsed time of this task in nanoseconds.longgetStartTimeNanos()Returns the start time of this task in nanoseconds, as reported bySystem.nanoTime().java.lang.StringgetTaskName()Returns the name of this task.inthashCode()Returns a hash code for this task, based on the task name.booleanisReentrant()Returns whether this task allows reentrant starts.static StopWatch.Taskstart(java.lang.String taskName)Creates and starts a new non-reentrant task with the given name.static StopWatch.Taskstart(java.lang.String taskName, boolean reentrant)Creates and starts a new task with the given name and reentrancy setting.voidstop()Stops this task and records the elapsed time since it was started.java.lang.StringtoString()Returns a string representation of this task, including the task name and elapsed time in nanoseconds.
-
-
-
Method Detail
-
start
public static StopWatch.Task start(java.lang.String taskName)
Creates and starts a new non-reentrant task with the given name.Example Usage
StopWatch.Task task = StopWatch.Task.start("validation"); // perform validation task.stop();- Parameters:
taskName- the name of the task- Returns:
- a new running
StopWatch.Taskinstance
-
start
public static StopWatch.Task start(java.lang.String taskName, boolean reentrant)
Creates and starts a new task with the given name and reentrancy setting.Example Usage
StopWatch.Task task = StopWatch.Task.start("retryableOp", true); // perform the operation task.stop(); System.out.println(task.isReentrant()); // true- Parameters:
taskName- the name of the taskreentrant-trueif the task may be started again while running- Returns:
- a new running
StopWatch.Taskinstance
-
stop
public void stop()
Stops this task and records the elapsed time since it was started.Example Usage
StopWatch.Task task = StopWatch.Task.start("encoding"); // perform encoding task.stop(); System.out.println("Elapsed: " + task.getElapsedNanos() + " ns");
-
getTaskName
public java.lang.String getTaskName()
Returns the name of this task.Example Usage
StopWatch.Task task = StopWatch.Task.start("sorting"); String name = task.getTaskName(); // "sorting"- Returns:
- the task name
-
isReentrant
public boolean isReentrant()
Returns whether this task allows reentrant starts.Example Usage
StopWatch.Task task = StopWatch.Task.start("retry", true); boolean reentrant = task.isReentrant(); // true- Returns:
trueif the task is reentrant,falseotherwise
-
getStartTimeNanos
public long getStartTimeNanos()
Returns the start time of this task in nanoseconds, as reported bySystem.nanoTime().Example Usage
StopWatch.Task task = StopWatch.Task.start("lookup"); long startNanos = task.getStartTimeNanos();- Returns:
- the start time in nanoseconds
-
getElapsedNanos
public long getElapsedNanos()
Returns the elapsed time of this task in nanoseconds. This value is zero untilstop()has been called.Example Usage
StopWatch.Task task = StopWatch.Task.start("render"); // perform rendering task.stop(); long elapsed = task.getElapsedNanos(); System.out.println("Render took " + elapsed + " ns");- Returns:
- the elapsed time in nanoseconds, or
0if the task has not been stopped
-
equals
public boolean equals(java.lang.Object o)
Compares this task with the specified object for equality. Two tasks are considered equal if they have the sametaskName.Example Usage
StopWatch.Task task1 = StopWatch.Task.start("compile"); StopWatch.Task task2 = StopWatch.Task.start("compile"); boolean equal = task1.equals(task2); // true- Overrides:
equalsin classjava.lang.Object- Parameters:
o- the object to compare with- Returns:
trueif the given object is aStopWatch.Taskwith the same name
-
hashCode
public int hashCode()
Returns a hash code for this task, based on the task name.Example Usage
StopWatch.Task task = StopWatch.Task.start("hash-demo"); int code = task.hashCode();- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code of this task
-
toString
public java.lang.String toString()
Returns a string representation of this task, including the task name and elapsed time in nanoseconds.Example Usage
StopWatch.Task task = StopWatch.Task.start("transform"); task.stop(); System.out.println(task.toString()); // Task[name='transform', elapsed(ns)=...]- Overrides:
toStringin classjava.lang.Object- Returns:
- a descriptive string representation of this task
-
-