Spring StopWatch

属性

  • taskList: 任务信息列表
  • keepTaskList: 是否保留任务信息列表
  • startTimeMillis: 任务开始的时间
  • currentTaskName: 任务名称
  • lastTaskInfo: 任务信息
  • taskCount: 任务数量
  • totalTimeMillis: 总共花费的时间

方法

  • org.springframework.util.StopWatch.start(java.lang.String)
  1. public void start(String taskName) throws IllegalStateException {
  2. if (this.currentTaskName != null) {
  3. throw new IllegalStateException("Can't start StopWatch: it's already running");
  4. }
  5. this.currentTaskName = taskName;
  6. this.startTimeMillis = System.currentTimeMillis();
  7. }
  • org.springframework.util.StopWatch.stop
  1. public void stop() throws IllegalStateException {
  2. if (this.currentTaskName == null) {
  3. throw new IllegalStateException("Can't stop StopWatch: it's not running");
  4. }
  5. // 消费的时间
  6. long lastTime = System.currentTimeMillis() - this.startTimeMillis;
  7. this.totalTimeMillis += lastTime;
  8. // 任务信息初始化
  9. this.lastTaskInfo = new TaskInfo(this.currentTaskName, lastTime);
  10. if (this.keepTaskList) {
  11. this.taskList.add(this.lastTaskInfo);
  12. }
  13. ++this.taskCount;
  14. this.currentTaskName = null;
  15. }