StopWatch类在几种工具类里都提供了任务执行监视,但个人觉得org.springframework.util.StopWatch;所提供的工具类是最好用的,所以今天介绍的是如何使用org.springframework.util.StopWatch;去纪录程序详细运行耗时,先一起来看看源码吧,如下:
package org.springframework.util;
import java.text.NumberFormat;
import java.util.LinkedList;
import java.util.List;
public class StopWatch {
private final String id;
private boolean keepTaskList;
private final List<StopWatch.TaskInfo> taskList;
private long startTimeMillis;
private boolean running;
private String currentTaskName;
private StopWatch.TaskInfo lastTaskInfo;
private int taskCount;
private long totalTimeMillis;
public StopWatch() {
this("");
}
public StopWatch(String id) {
this.keepTaskList = true;
this.taskList = new LinkedList();
this.id = id;
}
public String getId() {
return this.id;
}
public void setKeepTaskList(boolean keepTaskList) {
this.keepTaskList = keepTaskList;
}
public void start() throws IllegalStateException {
this.start("");
}
public void start(String taskName) throws IllegalStateException {
if(this.running) {
throw new IllegalStateException("Can\'t start StopWatch: it\'s already running");
} else {
this.running = true;
this.currentTaskName = taskName;
this.startTimeMillis = System.currentTimeMillis();
}
}