Spring中的StopWatch记录操作时间代码实例
StopWatch记录操作时间
说明
spring-framework提供的一个StopWatch类可以做类似任务执行时间控制,也就是封装了一个对开始时间,结束时间记录操作的Java类。
示例
start开始记录,stop停止记录,然后通过StopWatch的prettyPrint方法,可直观的输出代码执行耗时,以及执行时间百分比。
public class TestStopWatch {
public static void main(String[] args) throws InterruptedException {
StopWatch sw = new StopWatch();
sw.start("doSomething1");
Thread.sleep(200);
sw.stop();
sw.start("doSomething2");
Thread.sleep(200);
sw.stop();
sw.start("doSomething3");
Thread.sleep(200);
sw.stop();
System.out.println(sw.prettyPrint());
}
}控制台打印结果如下
StopWatch '': running time = 613210100 ns
---------------------------------------------
ns % Task name
---------------------------------------------
201980400 033% doSomething1
201809600 033% doSomething2
209420100 034% doSomething3
Process finished with exit code 0
到此这篇关于Spring中的StopWatch记录操作时间代码实例的文章就介绍到这了,更多相关StopWatch记录操作时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
关于JAVA中stream流的基础处理(获取对象字段和对象批量处理等)
这篇文章主要介绍了关于JAVA中stream流的基础处理,包含获取对象字段、按字段排序、按字段去重、对象批量处理、指定字段转数组等内容,需要的朋友可以参考下2023-03-03
详解Spring注解--@Autowired、@Resource和@Service
本篇文章主要介绍最重要的三个Spring注解,也就是@Autowired、@Resource和@Service,具有很好的参考价值。下面跟着小编一起来看下吧2017-05-05
SpringBoot2.7.x将logback升级到1.3.x以上版本的全过程解析
这篇文章给大家介绍SpringBoot:SpringBoot2.7.x如何将logback升级到1.3.x以上版本,本文给大家分享报错原因及解决方案,感兴趣的朋友跟随小编一起看看吧2026-04-04


最新评论