赞
踩
run 方法第第一步就是 创建一个StopWatch,然后启动
public ConfigurableApplicationContext run(String... args) {
// 1、
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// 略 ...
}
/****** org.springframework.util.StopWatch ******/
public StopWatch() {
this("");
}
private final String id;// 唯一标识
public StopWatch(String id) {
this.id = id;
}
简单不赘述
开始计时
/****** org.springframework.util.StopWatch ******/
public void start() throws IllegalStateException {
start("");
}
private boolean running;// 是否正在运行
private String currentTaskName;// 当前任务名称
private long startTimeMillis;// 当前任务开始时间
public void start(String taskName) throws IllegalStateException {
if (this.running) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.running = true;
this.currentTaskName = taskName;
this.startTimeMillis = System.currentTimeMillis();
}
简单不赘述
/****** org.springframework.util.StopWatch ******/
private long totalTimeMillis;// 累计耗时
private boolean keepTaskList = true;
private final List<TaskInfo> taskList = new LinkedList<TaskInfo>();
private int taskCount; // 任务计数器
public void stop() throws IllegalStateException {
if (!this.running) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
this.totalTimeMillis += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(lastTaskInfo);
}
++this.taskCount;
this.running = false;// 设置运行状态为停止
this.currentTaskName = null;
}
/****** org.springframework.util.StopWatch$TaskInfo ******/
public static final class TaskInfo {
private final String taskName;
private final long timeMillis;
TaskInfo(String taskName, long timeMillis) {
this.taskName = taskName;
this.timeMillis = timeMillis;
}
}
简单不赘述
整个代码很简单,StopWatch就是一个码表,可以反复启动停止。
停止时会将记录运行时间,然后创建一个任务(TaskInfo)放入码表的List属性中。
我们看到第 13 步 ,用到了StopWatch 对象,具体作用参考 StartupInfoLogger.logStarted
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。