public class StopWatch extends Object
此工具来自:https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/util/StopWatch.java
使用方法如下:
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 |
|---|---|
static StopWatch |
create(String id)
创建计时任务(秒表)
|
String |
currentTaskName()
获取当前任务名,
null 表示无任务 |
String |
getId()
获取StopWatch 的ID,用于多个秒表对象的区分
|
StopWatch.TaskInfo |
getLastTaskInfo()
获取最后的任务对象
|
String |
getLastTaskName()
获取最后的任务名
|
long |
getLastTaskTimeMillis()
获取最后任务的花费时间(毫秒)
|
long |
getLastTaskTimeNanos()
获取最后任务的花费时间(纳秒)
|
int |
getTaskCount()
获取任务数
|
StopWatch.TaskInfo[] |
getTaskInfo()
获取任务列表
|
long |
getTotal(TimeUnit unit)
获取所有任务的总花费时间
|
long |
getTotalTimeMillis()
获取所有任务的总花费时间(毫秒)
|
long |
getTotalTimeNanos()
获取所有任务的总花费时间(纳秒)
|
double |
getTotalTimeSeconds()
获取所有任务的总花费时间(秒)
|
boolean |
isRunning()
检查是否有正在运行的任务
|
String |
prettyPrint()
生成所有任务的一个任务花费时间表,单位纳秒
|
String |
prettyPrint(TimeUnit unit)
生成所有任务的一个任务花费时间表
|
void |
setKeepTaskList(boolean keepTaskList)
设置是否在停止后保留任务,
false 表示停止运行后不保留任务 |
String |
shortSummary()
获取任务信息,类似于:
StopWatch '[id]': running time = [total] ns
|
String |
shortSummary(TimeUnit unit)
获取任务信息,类似于:
StopWatch '[id]': running time = [total] [unit]
|
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 static StopWatch create(String id)
id - 用于标识秒表的唯一IDpublic String getId()
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 getTotal(TimeUnit unit)
unit - 时间单位,null表示默认TimeUnit.NANOSECONDSpublic long getTotalTimeNanos()
getTotalTimeMillis(),
getTotalTimeSeconds()public long getTotalTimeMillis()
getTotalTimeNanos(),
getTotalTimeSeconds()public double getTotalTimeSeconds()
getTotalTimeNanos(),
getTotalTimeMillis()public int getTaskCount()
public StopWatch.TaskInfo[] getTaskInfo()
public String shortSummary()
StopWatch '[id]': running time = [total] ns
public String shortSummary(TimeUnit unit)
StopWatch '[id]': running time = [total] [unit]
unit - 时间单位,null则默认为TimeUnit.NANOSECONDSpublic String prettyPrint()
public String prettyPrint(TimeUnit unit)
unit - 时间单位,null则默认TimeUnit.NANOSECONDS 纳秒Copyright © 2025. All rights reserved.