work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Spring Boot计时器

Posted on 2020-02-27 11:58  work hard work smart  阅读(2277)  评论(0编辑  收藏  举报

Sping Boot计时器

用来统计任务的耗时

 

1、进入run方法,其中StopWatch就是计时器

 

2、计时器的使用

@RunWith(SpringRunner.class)
@SpringBootTest
public class StopWatchTest {
    
    @Test
    public void testStopWatch() throws InterruptedException {
        StopWatch myWatch = new StopWatch("myWatch");
        myWatch.start("task1");
        Thread.sleep(2000L);
        myWatch.stop();

        myWatch.start("task2");
        Thread.sleep(3000L);
        myWatch.stop();

        myWatch.start("task3");
        Thread.sleep(1000L);
        myWatch.stop();

        System.out.println( myWatch.prettyPrint());


    }



}

  

运行testStopWatch方法,输出结果如下

StopWatch 'myWatch': running time (millis) = 6003
-----------------------------------------
ms     %     Task name
-----------------------------------------
02000  033%  task1
03002  050%  task2
01001  017%  task3