StopWatch 统计时间

package com.example.dyreportapi.util;
import org.springframework.util.StopWatch;
/**
 * @Author:Kevin
 * @Date:2023/8/17 10:28
 */
public class StopWatchTest {

    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch("字符串操作时间统计");
        stopWatch.start("统计字符串长度");
        String str = "hello world";
        int len = str.length();
        stopWatch.stop();
        System.out.println("task count:" + stopWatch.getTaskCount());
        System.out.println("last task name:" + stopWatch.getLastTaskName());
        System.out.println("total time:" + stopWatch.getTotalTimeMillis() + " ms");
        System.out.println("last task time:" + stopWatch.getLastTaskTimeMillis() + " ms");
        stopWatch.start("将字符串转换成大写");
        String upper = str.toUpperCase();
        stopWatch.stop();
        System.out.println("task count:" + stopWatch.getTaskCount());
        System.out.println("last task name:" + stopWatch.getLastTaskName());
        System.out.println("total time:" + stopWatch.getTotalTimeMillis() + " ms");
        System.out.println("last task time:" + stopWatch.getLastTaskTimeMillis() + " ms");
        stopWatch.start("将字符串转换成小写");
        String lower = str.toLowerCase();
        stopWatch.stop();

        System.out.println("task count:" + stopWatch.getTaskCount());
        System.out.println("last task name:" + stopWatch.getLastTaskName());
        System.out.println("total time:" + stopWatch.getTotalTimeMillis() + " ms");
        System.out.println("last task time:" + stopWatch.getLastTaskTimeMillis() + " ms");
    }
}

### Java 中使用 StopWatch 实现多线程应用的性能计时 为了在 Java 多线程环境中有效地使用 `StopWatch` 记录各个任务的执行时间,可以采用如下方式: #### 创建并启动 StopWatch 对象 ```java import org.springframework.util.StopWatch; public class MultiThreadedStopWatchExample { private static final int THREAD_COUNT = 5; public static void main(String[] args) throws InterruptedException { StopWatch stopWatch = new StopWatch(); // 启动总定时器 stopWatch.start("Overall Execution"); ``` #### 定义线程任务 对于每一个单独的任务,在其内部也创建独立的 `StopWatch` 实例以便精确跟踪该特定任务所花费的时间。 ```java Runnable taskRunner = () -> { try (var localStopWatch = new StopWatch()) { String threadName = Thread.currentThread().getName(); localStopWatch.start(threadName); // 模拟工作负载 Thread.sleep((long)(Math.random() * 1000)); localStopWatch.stop(); System.out.printf("%s completed in %d ms\n", threadName, localStopWatch.getLastTaskTimeMillis()); } }; ``` #### 提交任务至线程池 通过 `ExecutorService` 或者其他形式的线程管理机制提交这些带有内置计时逻辑的任务给多个工作者线程去完成。 ```java try (var executorService = java.util.concurrent.Executors.newFixedThreadPool(THREAD_COUNT)) { for (int i = 0; i < THREAD_COUNT; ++i) executorService.submit(taskRunner); // 阻塞直到所有任务结束 executorService.shutdown(); while (!executorService.isTerminated()) ; } finally { stopWatch.stop(); } // 输出总体耗时以及各子任务详情 System.out.println(stopWatch.prettyPrint()); System.out.println("Total execution time: " + stopWatch.getTotalTimeMillis() + "ms"); ``` 这种方法不仅能够监控整个应用程序层面的整体表现,还能深入分析单个操作的具体开销。这有助于识别潜在瓶颈所在之处,并据此优化代码效率[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值