public class StopWatch extends Object
StopWatch stopWatch = new StopWatch("任务名称");
// 任务1
stopWatch.start("任务一");
Thread.sleep(1000);
stopWatch.stop();
// 任务2
stopWatch.start("任务一");
Thread.sleep(2000);
stopWatch.stop();
// 打印出耗时
Console.log(stopWatch.prettyPrint());
| Modifier and Type | Class and Description |
|---|---|
static class |
StopWatch.TaskInfo
存放任务名称和花费时间对象
|
| Constructor and Description |
|---|
StopWatch()
构造,不启动任何任务
|
StopWatch(String id)
构造,不启动任何任务
|
StopWatch(String id,
boolean keepTaskList)
构造,不启动任何任务
|
| Modifier and Type | Method and Description |
|---|---|
String |
currentTaskName()
获取当前任务名,
null 表示无任务 |
String |
getId()
获取
StopWatch 的ID,用于多个秒表对象的区分 |
StopWatch.TaskInfo |
getLastTaskInfo()
获取最后的任务对象
|
String |
getLastTaskName()
获取最后的任务名
|
long |
getLastTaskTimeMillis()
获取最后任务的花费时间(毫秒)
|
long |
getLastTaskTimeNanos()
获取最后任务的花费时间(纳秒)
|
int |
getTaskCount()
获取任务数
|
StopWatch.TaskInfo[] |
getTaskInfo()
获取任务列表
|
long |
getTotalTimeMillis()
获取所有任务的总花费时间(毫秒)
|
long |
getTotalTimeNanos()
获取所有任务的总花费时间(纳秒)
|
double |
getTotalTimeSeconds()
获取所有任务的总花费时间(秒)
|
boolean |
isRunning()
检查是否有正在运行的任务
|
String |
prettyPrint()
生成所有任务的一个任务花费时间表
|
void |
setKeepTaskList(boolean keepTaskList)
设置是否在停止后保留任务,
false 表示停止运行后不保留任务 |
String |
shortSummary()
获取任务信息
|
void |
start()
开始默认的新任务
|
void |
start(String taskName)
开始指定名称的新任务
|
void |
stop()
停止当前任务
|
String |
toString() |
public StopWatch()
public StopWatch(String id)
id - 用于标识秒表的唯一IDpublic StopWatch(String id, boolean keepTaskList)
id - 用于标识秒表的唯一IDkeepTaskList - 是否在停止后保留任务,false 表示停止运行后不保留任务public String getId()
StopWatch 的ID,用于多个秒表对象的区分StopWatch(String)public void setKeepTaskList(boolean keepTaskList)
false 表示停止运行后不保留任务keepTaskList - 是否在停止后保留任务public void start()
throws IllegalStateException
IllegalStateException - 前一个任务没有结束public void start(String taskName) throws IllegalStateException
taskName - 新开始的任务名称IllegalStateException - 前一个任务没有结束public void stop()
throws IllegalStateException
IllegalStateException - 任务没有开始public boolean isRunning()
currentTaskName()public String currentTaskName()
null 表示无任务null 表示无任务isRunning()public long getLastTaskTimeNanos()
throws IllegalStateException
IllegalStateException - 无任务public long getLastTaskTimeMillis()
throws IllegalStateException
IllegalStateException - 无任务public String getLastTaskName() throws IllegalStateException
IllegalStateException - 无任务public StopWatch.TaskInfo getLastTaskInfo() throws IllegalStateException
StopWatch.TaskInfo 任务对象,包括任务名和花费时间IllegalStateException - 无任务public long getTotalTimeNanos()
getTotalTimeMillis(),
getTotalTimeSeconds()public long getTotalTimeMillis()
getTotalTimeNanos(),
getTotalTimeSeconds()public double getTotalTimeSeconds()
getTotalTimeNanos(),
getTotalTimeMillis()public int getTaskCount()
public StopWatch.TaskInfo[] getTaskInfo()
public String shortSummary()
public String prettyPrint()
Copyright © 2019. All rights reserved.