Master Java Performance Timing with Spring and Apache StopWatch
This article explains how to use Spring's org.springframework.util.StopWatch and Apache Commons Lang3 StopWatch to accurately measure and analyze Java code execution time, covering setup, basic usage, advanced features, thread‑safety considerations, and practical code examples for both libraries.
Background
When developing Java applications, measuring the execution time of tasks is often needed. The naive approach of recording start and end timestamps works but becomes cumbersome for multiple sections of code.
Spring StopWatch Usage
The org.springframework.util.StopWatch class provides a convenient way to time sequential tasks with millisecond precision. It is suitable for single‑threaded code blocks.
public static void main(String[] args) throws InterruptedException {
StopWatch stopWatch = new StopWatch();
// Task 1 – sleep 3 seconds
stopWatch.start("TaskOne");
Thread.sleep(1000 * 3);
stopWatch.stop();
// Task 2 – sleep 10 seconds
stopWatch.start("TaskTwo");
Thread.sleep(1000 * 10);
stopWatch.stop();
// Task 3 – sleep 10 seconds
stopWatch.start("TaskThree");
Thread.sleep(1000 * 10);
stopWatch.stop();
System.out.println(stopWatch.prettyPrint());
System.out.println(stopWatch.shortSummary());
System.out.println(stopWatch.getLastTaskName());
System.out.println(stopWatch.getLastTaskInfo());
System.out.println("Total time: " + stopWatch.getTotalTimeMillis());
System.out.println("Task count: " + stopWatch.getTaskCount());
System.out.println("All tasks: " + stopWatch.getTaskInfo());
}Key points:
The StopWatch is not thread‑safe; use separate instances for concurrent timing.
Only one task can be active at a time; you must stop a task before starting another.
Apache Commons Lang3 StopWatch Usage
The org.apache.commons.lang3.time.StopWatch offers similar functionality with additional features such as split, suspend, and resume.
public static void main(String[] args) throws InterruptedException {
StopWatch watch = StopWatch.createStarted();
Thread.sleep(1000);
System.out.println("Elapsed: " + watch.getTime() + "ms");
watch.split();
System.out.println("Split time: " + watch.getSplitTime());
watch.suspend();
Thread.sleep(2000); // paused
watch.resume();
Thread.sleep(1000);
watch.stop();
System.out.println("Total time: " + watch.getTime() + "ms");
System.out.println("Total seconds: " + watch.getTime(TimeUnit.SECONDS) + "s");
}Important notes:
It supports split timing, suspension, and resumption.
After calling reset(), the stopwatch can be reused.
Conclusion
Using dedicated stopwatch utilities simplifies performance analysis and makes timing code more readable and maintainable compared to manual timestamp calculations. Choose Spring's StopWatch for simple sequential tasks and Apache's StopWatch when you need advanced features like split and suspend.
Source: blog.csdn.net/duleilewuhen/article/details/114379693
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
