spring工具-StopWatch使用介绍

StopWatch是Spring核心包中的一个工具类,它是一个简单的秒表工具,可以计时指定代码段的运行时间以及汇总这个运行时间,
使用它可以隐藏使用 System.currentTimeMillis() ,提高应用程序代码的可读性并减少计算错误的可能性。

注意事项
StopWatch对象不是设计为线程安全的,并且不使用同步。

使用场景
一般是在开发过程中验证性能,而不是作为生产应用程序的一部分

在这里插入图片描述

package com.wfg.boot.spring;

import org.springframework.util.StopWatch;

import java.util.Arrays;

/**
 * @author wufagang
 * @description
 * @date 2023年09月15日 20:48
 */
public class StopWatchTest {


    public static void main(String[] args) throws InterruptedException {
        StopWatch stopWatch = new StopWatch("测试秒表");

        stopWatch.start("暂停100毫秒");
        Thread.sleep(100 * 1);
        stopWatch.stop();

        stopWatch.start("暂停200毫秒");
        Thread.sleep(100 * 2);
        stopWatch.stop();

        stopWatch.start("暂停300毫秒");
        Thread.sleep(100 * 3);
        stopWatch.stop();

        stopWatch.setKeepTaskList(true); //是否构建TaskInfo信息
        Arrays.stream(stopWatch.getTaskInfo()).forEach(sw ->
                System.out.println(sw.getTaskName()+" "+
                        sw.getTimeMillis()+" "+sw.getTimeSeconds()));
        // 在start()方法和stop()方法间时,isRunning()返回true
        System.out.println(stopWatch.isRunning());
        System.out.println(stopWatch.prettyPrint());//打印详细信息
        System.out.println(stopWatch.shortSummary());//打印简要信息

    }


}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值