简单使用优雅的程序计数器-StopWatch

一、引入hutool-core 5.8.18包
二、代码
public static void main(String[] args) throws InterruptedException {
    StopWatch stopWatch = new StopWatch("测试StopWatch");
    stopWatch.start("任务1");
    // 任务1花费1000毫秒
    Thread.sleep(1000);
    stopWatch.stop();//必须要stop,不然下一个任务无法开始计时

    stopWatch.start("任务2");
    // 任务2花费2000毫秒
    Thread.sleep(2000);
    stopWatch.stop();
    System.out.println(stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
}
三、输出:总耗时、每个任务耗时、耗时占比、任务名称

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
hutool stopwatch是一个在Java开发中用于测量代码执行时间的工具。它提供了一个StopWatch类,可以方便地计时并打印出执行时间。在使用hutool stopwatch时,你可以使用Start和Stop方法来启动和停止计时器,然后可以使用prettyPrint方法来打印出耗时情况。 在SpringBoot中,也使用StopWatch来计时,可以在程序启动时使用StopWatch实例来记录任务的启动和停止时间,以便于监控程序的运行情况。 除了hutool stopwatch,还有一个名为guava stopwatch的工具,它也提供了类似的功能,可以用于测量代码执行时间。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [耗时统计(JAVA)](https://blog.csdn.net/sunquan291/article/details/125653617)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [StopWatch—— 优雅的程序计时器](https://blog.csdn.net/weixin_40920359/article/details/126038214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值