springboot stopwatch
时间: 2023-12-28 18:25:41 浏览: 28
以下是使用Spring的StopWatch类来计算总耗时的示例代码:
```java
import org.springframework.util.StopWatch;
public class SpringStopWatchExample {
public static void main(String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start();
// 模拟长时间任务
Thread.sleep(1000);
sw.stop();
System.out.println(sw.getTotalTimeMillis());
}
}
```
这段代码使用了Spring的StopWatch类来计算代码执行的总耗时。首先,我们创建了一个StopWatch对象,并调用start()方法开始计时。然后,我们模拟了一个长时间的任务,使用Thread.sleep()方法让程序暂停1秒钟。最后,我们调用stop()方法停止计时,并使用getTotalTimeMillis()方法获取总耗时,并将结果打印出来。
你可以在Spring的官方文档中了解更多关于StopWatch类的信息:[https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StopWatch.html](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/util/StopWatch.html)