Breaking News
Home / Java / Java Tutorials / Build a simple Stop Watch Timer

Build a simple Stop Watch Timer

Stop Watch in JavaAt times we need to know how much time is taken to do a Task? This task can be any unit of work e.g. a method, a few lines of code, a long running code etc. This can be helpful to understand time taken to do a Task and tune it for performance reason.

A Simple Stop Watch utility can help here to do this where ever we want to make a time measurement. This utility is Thread Safe and multiple watches can be in used in parallel with no performance impact. Each task that needs to put on a stopwatch  is stored into Hashtable.

To measure the task time, invoke StopWatch.start(“TASK-NAME”);

Then execute the Task and invoke StopWatch.stop(“TASK-NAME”); to stop the watch. The return value of the stop method gives the time it took to execute the task.

Stop Watch Timer Implementation

Simple Stop Watch Implementation

The following is the a simple implementation of a Stop Watch like Timer.

 [pb_builder]

Check Also

Have a Question

How to select a JVM Analyzing Tool?

A JVM monitoring tool also know as a Diagnostic might be needed to fine tune …

Advertisment ad adsense adlogger