Skip to content

io microsphere util StopWatch

github-actions[bot] edited this page Mar 21, 2026 · 15 revisions

StopWatch

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/StopWatch.java

Overview

StopWatch provides a simple way to measure execution time for tasks, supporting nested task tracking. Each task can be started with optional reentrancy control via #start(String, boolean).

By default, tasks are non-reentrant. Attempting to start a non-reentrant task while it's already running will throw an IllegalStateException. If reentrancy is enabled, subsequent calls to start the same task will be ignored until it is stopped.

Example Usage

`// Basic usage
StopWatch stopWatch = new StopWatch("MyStopWatch");
stopWatch.start("Task 1");
// perform operations
stopWatch.stop();
System.out.println(stopWatch);  // Outputs: StopWatch[id='MyStopWatch', running tasks=[], completed tasks=[Task[name='Task 1', elapsed(ns)=...]], totalTime(ns)=...]

// Nested tasks
stopWatch.start("Task A");
// do something
stopWatch.start("Task B");
// do something else
stopWatch.stop();  // stops Task B
stopWatch.stop();  // stops Task A

// Reentrant task example
stopWatch.start("Reentrant Task", true);
// do something
stopWatch.start("Reentrant Task", true);  // this call is ignored
// ...
stopWatch.stop();  // ends the original task
`

Note: This class is not thread-safe and should only be used within a single thread.

Declaration

public class StopWatch

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

// Basic usage
StopWatch stopWatch = new StopWatch("MyStopWatch");
stopWatch.start("Task 1");
// perform operations
stopWatch.stop();
System.out.println(stopWatch);  // Outputs: StopWatch[id='MyStopWatch', running tasks=[], completed tasks=[Task[name='Task 1', elapsed(ns)=...]], totalTime(ns)=...]

// Nested tasks
stopWatch.start("Task A");
// do something
stopWatch.start("Task B");
// do something else
stopWatch.stop();  // stops Task B
stopWatch.stop();  // stops Task A

// Reentrant task example
stopWatch.start("Reentrant Task", true);
// do something
stopWatch.start("Reentrant Task", true);  // this call is ignored
// ...
stopWatch.stop();  // ends the original task

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.StopWatch;

API Reference

Public Methods

Method Description
start
start
stop
getCurrentTask
getId
getRunningTasks
getCompletedTasks
getTotalTimeNanos
getTotalTime
start
start
stop
getTaskName
isReentrant
getStartTimeNanos
getElapsedNanos

This documentation was auto-generated from the source code of microsphere-java.

Home

annotation-processor

java-annotations

java-core

java-test

jdk-tools

lang-model

Clone this wiki locally