|
|
|
|
# Spring StopWatch
|
|
|
|
|
- Author: [HuiFer](https://github.com/huifer)
|
|
|
|
|
- Դ<><D4B4><EFBFBD>Ķ<EFBFBD><C4B6>ֿ<EFBFBD>: [SourceHot-spring](https://github.com/SourceHot/spring-framework-read)
|
|
|
|
|
|
|
|
|
|
- ȫ·<C8AB><C2B7>: `org.springframework.util.StopWatch`
|
|
|
|
|
## <20><><EFBFBD><EFBFBD>
|
|
|
|
|
- taskList: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>б<EFBFBD>
|
|
|
|
|
- keepTaskList: <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>б<EFBFBD>
|
|
|
|
|
- startTimeMillis: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1>
|
|
|
|
|
- currentTaskName: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
- lastTaskInfo: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
- taskCount: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
- totalTimeMillis: <20>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD>ѵ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
|
|
|
|
## <20><><EFBFBD><EFBFBD>
|
|
|
|
|
- `org.springframework.util.StopWatch.start(java.lang.String)`
|
|
|
|
|
```java
|
|
|
|
|
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.startTimeMillis = System.currentTimeMillis();
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
- `org.springframework.util.StopWatch.stop`
|
|
|
|
|
```java
|
|
|
|
|
public void stop() throws IllegalStateException {
|
|
|
|
|
if (this.currentTaskName == null) {
|
|
|
|
|
throw new IllegalStateException("Can't stop StopWatch: it's not running");
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD>ѵ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
long lastTime = System.currentTimeMillis() - this.startTimeMillis;
|
|
|
|
|
this.totalTimeMillis += lastTime;
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>ʼ<EFBFBD><CABC>
|
|
|
|
|
this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
|
|
|
|
|
if (this.keepTaskList) {
|
|
|
|
|
this.taskList.add(this.lastTaskInfo);
|
|
|
|
|
}
|
|
|
|
|
++this.taskCount;
|
|
|
|
|
this.currentTaskName = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
```
|