package com.example.stopwatch;

import org.springframework.util.StopWatch;


public class TestStopWatch {
    private void test() throws InterruptedException {
        StopWatch sw = new StopWatch();

        sw.start("起床");
        Thread.sleep(1000);
        sw.stop();

        sw.start("洗漱");
        Thread.sleep(2000);
        sw.stop();

        sw.start("锁门");
        Thread.sleep(500);
        sw.stop();

        System.out.println(sw.prettyPrint());
        System.out.println(sw.getTotalTimeMillis());
        System.out.println(sw.getLastTaskName());
        System.out.println(sw.getLastTaskInfo());
        System.out.println(sw.getTaskCount());
    }


    public static void main(String []argv) throws InterruptedException {
        TestStopWatch testStopWatch = new TestStopWatch();
        testStopWatch.test();
    }
}

结果

1
2
3
4
5
6
7
8
9
10
11
12
StopWatch '': running time (millis) = 3518
-----------------------------------------
ms     %     Task name
-----------------------------------------
00998  028%  起床
02020  057%  洗漱
00500  014%  锁门
 
3518
锁门
org.springframework.util.StopWatch$TaskInfo@5b2133b1
3