【StopWatch】Spring框架中提供的任务时间监控类

使用代码以下java

import org.springframework.util.StopWatch;
public class ApplicationTest4 {
    public static void main(String[] args) throws Exception {
        StopWatch watch = new StopWatch("这是一个测试用的StopWatch");
        watch.start("起床");
        Thread.sleep(1000);
        watch.stop();

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

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

        System.out.println(watch.prettyPrint());
        System.out.println(watch.getLastTaskName());
        System.out.println(watch.getTaskCount());
        System.out.println(watch.getLastTaskInfo());

    }
}

运行输出结果见下:spring

StopWatch '这是一个测试用的StopWatch': running time (millis) = 3503
-----------------------------------------
ms     %     Task name
-----------------------------------------
01001  029%  起床
02001  057%  洗漱
00501  014%  锁门

锁门
3
org.springframework.util.StopWatch$TaskInfo@3af49f1c

注意是在org.springframework.util 包下测试

方法也很好理解code

watch.prettyPrint()  打印完整的任务监控信息对象

watch.getLastTaskName() 获取上一个任务的名称get

watch.getTaskCount() 获取任务个数io

watch.getLastTaskInfo() 获取上一个任务对象ast

相关文章
相关标签/搜索