StopWatch介绍
- StopWatch是spring-core包中的工具类,它是一个秒表工具,可以计时指定代码块的运行时间,做性能分析等等
- 可以隐藏System.currentTimeMillis(),提高代码的可读性,减少计算错误的可能性
-
StopWatch不保证线程安全
StopWatch测试
@Test public void test() throws InterruptedException { StopWatch stopWatch = new StopWatch("任务计时测试"); stopWatch.start("任务1"); Thread.sleep(1000L); stopWatch.stop(); stopWatch.start("任务2"); Thread.sleep(1500L); stopWatch.stop(); stopWatch.start("任务3"); Thread.sleep(2000L); System.out.println(stopWatch.getLastTaskName() + ",是否正在运行中:" + stopWatch.isRunning()); stopWatch.stop(); System.out.println("StopWatch是否正在运行中:" + stopWatch.isRunning()); // 总计耗时 System.out.println("总计耗时:" + stopWatch.getTotalTimeMillis() + "毫秒"); System.out.println("总计耗时:" + stopWatch.getTotalTimeSeconds() + "秒"); // 分别统计每个任务耗时 Arrays.stream(stopWatch.getTaskInfo()).forEach(sw -> System.out.println(sw.getTaskName() + ",耗时" + sw.getTimeSeconds() + "秒")); // 打印详细信息 System.out.println(stopWatch.prettyPrint()); }
StopWatch方法
- spring version:5.3.18
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
标签:String,System,stopWatch,任务,StopWatch,秒表 From: https://www.cnblogs.com/coolw/p/16619045.html