public final class Stopwatchs extends Object
Invokes method start(java.lang.String) for timing a task, invokes method end() for stop timing the
corresponding task, the start and end must match exactly.
Note: Remember to release a stopwatch if finished statistics finally to avoid memory leak.
See the following example snippet codes:
Stopwatchs.start("task 1");
// task 1 includes three sub-tasks: task 1.1, task 1.2 and task 1.3
// .... task 1 processing ....
Stopwatchs.start("task 1.1");
// .... task 1.1 processing ....
Stopwatchs.end(); // Ends 1.1
Stopwatchs.start("task 1.2");
// task 1.2 includes two sub-tasks: task 1.2.1 and task 1.2.2
// .... task 1.2 processing ....
Stopwatchs.start("task 1.2.1");
// .... task 1.2.1 processing ....
Stopwatchs.end(); // Ends 1.2.1
Stopwatchs.start("task 1.2.2");
// .... task 1.2.2 processing ....
Stopwatchs.end(); // Ends 1.2.2
Stopwatchs.end(); // Ends 1.2
Stopwatchs.start("task 1.3");
// .... task 1.3 processing ....
Stopwatchs.end(); // Ends 1.3
Stopwatchs.end(); // Ends 1
// Shows the timing statistics
System.out.println(Stopwatchs.getTimingStat());
Stopwatchs.release();
Outputs:
[100.0]%, [80]ms [task 1]
[62.5]%, [50]ms [task 1.1]
[25.0]%, [20]ms [task 1.2]
[0.0]%, [0]ms [task 1.2.1]
[25.0]%, [20]ms [task 1.2.2]
[12.5]%, [10]ms [task 1.3]
Stopwatchs.Stopwatch,
getTimingStat()| Modifier and Type | Method and Description |
|---|---|
static void |
end()
Ends the timing of the recent task started by
start(java.lang.String). |
static long |
getElapsed(String taskTitle)
Gets elapsed time from the specified parent stopwatch with the specified task title.
|
static String |
getTimingStat()
Gets the current timing statistics.
|
static void |
release()
Releases the thread-local stopwatch.
|
static void |
start(String taskTitle)
Starts a task timing with the specified task title.
|
public static void start(String taskTitle)
taskTitle - the specified task titlepublic static void end()
start(java.lang.String).public static void release()
Sets the thread-local stopwatch with null.
public static String getTimingStat()
If a task is not ended, the outputs will be minus for percentage and elapsed, the absolute value of the elapsed filed is the start time.
"No stopwatch" if not stopwatchpublic static long getElapsed(String taskTitle)
taskTitle - the specified task title-1 if not found any stopwatch corresponding to the specified task titleCopyright © 2009–2019 B3log. All rights reserved.