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记录操作时间内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Boot mybatis-config 和 log4j 输出sql 日志的方式
这篇文章主要介绍了Spring Boot mybatis-config 和 log4j 输出sql 日志的方式,本文通过实例图文相结合给大家介绍的非常详细,需要的朋友可以参考下2021-07-07


最新评论