1.作用
- 用来记录任务的执行耗时
- org.springframework.util.StopWatch
在org.springframework.boot.SpringApplication#run(java.lang.String…)中用到了计时器,用来计时spring 框架启动耗时

打印启动时间
private CharSequence getStartedMessage(StopWatch stopWatch) {
StringBuilder message = new StringBuilder();
message.append("Started ");
appendApplicationName(message);
message.append(" in ");
message.append(stopWatch.getTotalTimeMillis() / 1000.0);
message.append(" seconds");
try {
double uptime = ManagementFactory.getRuntimeMXBean().getUptime() / 1000.0;
message.append(" (JVM running for ").append(uptime).append(")");
}
catch (Throwable ex) {
// No JVM time available
}
return message;
}

2.计时器源码
构造函数
public StopWatch() {
this("");
}
public StopWatch(String id) {
this.id = id;
}
传入计时器的id,如果不传入则为空
开启计时器
public void start(String taskName) throws IllegalStateException {
if (this.currentTaskName != null) {
throw new IllegalStateException("Can't start StopWatch: it's already running");
}
this.currentTaskName = taskName;
this.startTimeNanos = System.nanoTime();
}
主要设置当前任务的名称,并记录下当时的时间戳
System.nanoTime:返回的是时间的纳秒值,用来来做高精度的时间计算
停止计时器
public void stop() throws IllegalStateException {
if (this.currentTaskName == null) {
throw new IllegalStateException("Can't stop StopWatch: it's not running");
}
long lastTime = System.nanoTime() - this.startTimeNanos;
this.totalTimeNanos += lastTime;
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
if (this.keepTaskList) {
this.taskList.add(this.lastTaskInfo);
}
++this.taskCount;
this.currentTaskName = null;
}
记录当前任务的用时,判断是否需要记录任务的明细,清空当前计时器
内部类:TaskInfo实现
public static final class TaskInfo {
private final String taskName;
private final long timeNanos;
TaskInfo(String taskName, long timeNanos) {
this.taskName = taskName;
this.timeNanos = timeNanos;
}
}
打印结果:prettyPrint
public String prettyPrint() {
StringBuilder sb = new StringBuilder(shortSummary());
sb.append('\n');
if (!this.keepTaskList) {
sb.append("No task info kept");
}
else {
sb.append("---------------------------------------------\n");
sb.append("ns % Task name\n");
sb.append("---------------------------------------------\n");
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumIntegerDigits(9);
nf.setGroupingUsed(false);
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (TaskInfo task : getTaskInfo()) {
sb.append(nf.format(task.getTimeNanos())).append(" ");
sb.append(pf.format((double) task.getTimeNanos() / getTotalTimeNanos())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
}
3.计时器使用
StopWatch myWatch = new StopWatch("myWatch");
myWatch.start("test1");
Thread.sleep(1000L);
myWatch.stop();
myWatch.start("test2");
Thread.sleep(2000L);
myWatch.stop();
myWatch.start("test3");
Thread.sleep(8000L);
myWatch.stop();
System.out.println(myWatch.prettyPrint());
- 结果
StopWatch 'myWatch': running time = 11002394708 ns
---------------------------------------------
ns % Task name
---------------------------------------------
1002167975 009% test1
2000082109 018% test2
8000144624 073% test3
本文介绍了SpringBoot中StopWatch的作用,它是用于记录任务执行耗时的工具类。在Spring框架启动时,StopWatch用于计算启动时间。文章详细解析了StopWatch的构造函数、开启、停止计时器的原理,以及如何使用StopWatch进行任务耗时的统计和结果的打印。
1万+

被折叠的 条评论
为什么被折叠?



