类 StopWatch
- java.lang.Object
- org.springframework.util.StopWatch
public class StopWatch extends Object
Simple stop watch, allowing for timing of a number of tasks, exposing total running time and running time for each named task.Conceals use of
System.currentTimeMillis()
, improving the readability of application code and reducing the likelihood of calculation errors.Note that this object is not designed to be thread-safe and does not use synchronization.
This class is normally used to verify performance during proof-of-concepts and in development, rather than as part of production applications.
- 从以下版本开始:
- May 2, 2001
- 作者:
- Rod Johnson, Juergen Hoeller, Sam Brannen
嵌套类概要
嵌套类 修饰符和类型 类 说明 static class
StopWatch.TaskInfo
Inner class to hold data about one task executed within the stop watch.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 String
currentTaskName()
Return the name of the currently running task, if any.String
getId()
Return the id of this stop watch, as specified on construction.StopWatch.TaskInfo
getLastTaskInfo()
Return the last task as a TaskInfo object.String
getLastTaskName()
Return the name of the last task.long
getLastTaskTimeMillis()
Return the time taken by the last task.int
getTaskCount()
Return the number of tasks timed.StopWatch.TaskInfo[]
getTaskInfo()
Return an array of the data for tasks performed.long
getTotalTimeMillis()
Return the total time in milliseconds for all tasks.double
getTotalTimeSeconds()
Return the total time in seconds for all tasks.boolean
isRunning()
Return whether the stop watch is currently running.String
prettyPrint()
Return a string with a table describing all tasks performed.void
setKeepTaskList(boolean keepTaskList)
Determine whether the TaskInfo array is built over time.String
shortSummary()
Return a short description of the total running time.void
start()
Start an unnamed task.void
start(String taskName)
Start a named task.void
stop()
Stop the current task.String
toString()
Return an informative string describing all tasks performed For custom reporting, callgetTaskInfo()
and use the task info directly.
构造器详细资料
StopWatch
public StopWatch()
Construct a new stop watch. Does not start any task.
方法详细资料
getId
public String getId()
Return the id of this stop watch, as specified on construction.- 返回:
- the id (empty String by default)
- 从以下版本开始:
- 4.2.2
- 另请参阅:
StopWatch(String)
setKeepTaskList
public void setKeepTaskList(boolean keepTaskList)
Determine whether the TaskInfo array is built over time. Set this to "false" when using a StopWatch for millions of intervals, or the task info structure will consume excessive memory. Default is "true".
start
public void start() throws IllegalStateException
Start an unnamed task. The results are undefined ifstop()
or timing methods are called without invoking this method.- 抛出:
IllegalStateException
- 另请参阅:
stop()
start
public void start(String taskName) throws IllegalStateException
Start a named task. The results are undefined ifstop()
or timing methods are called without invoking this method.- 参数:
taskName
- the name of the task to start- 抛出:
IllegalStateException
- 另请参阅:
stop()
stop
public void stop() throws IllegalStateException
Stop the current task. The results are undefined if timing methods are called without invoking at least one pairstart()
/stop()
methods.- 抛出:
IllegalStateException
- 另请参阅:
start()
isRunning
public boolean isRunning()
Return whether the stop watch is currently running.- 另请参阅:
currentTaskName()
currentTaskName
public String currentTaskName()
Return the name of the currently running task, if any.- 从以下版本开始:
- 4.2.2
- 另请参阅:
isRunning()
getLastTaskTimeMillis
public long getLastTaskTimeMillis() throws IllegalStateException
Return the time taken by the last task.
getLastTaskName
public String getLastTaskName() throws IllegalStateException
Return the name of the last task.
getLastTaskInfo
public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException
Return the last task as a TaskInfo object.
getTotalTimeMillis
public long getTotalTimeMillis()
Return the total time in milliseconds for all tasks.
getTotalTimeSeconds
public double getTotalTimeSeconds()
Return the total time in seconds for all tasks.
getTaskCount
public int getTaskCount()
Return the number of tasks timed.
getTaskInfo
public StopWatch.TaskInfo[] getTaskInfo()
Return an array of the data for tasks performed.
shortSummary
public String shortSummary()
Return a short description of the total running time.
prettyPrint
public String prettyPrint()
Return a string with a table describing all tasks performed. For custom reporting, call getTaskInfo() and use the task info directly.