How do I monitor times using a StopWatch?
Author: Deron Eriksson
Description: This Java tutorial describes how to use a StopWatch to perform timing activities.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page:    1 2 >

The StopWatch class in the Commons LangS library offers a handy way of timing various activities in your code. After creating a StopWatch object, you start it with a call to start(). You can pause the stopwatch with a call to suspend(), and you can start the paused stopwatch up again with a call to resume(). You can call split() to get a split time, which you can view in a readable format by calling toSplitString(). When you call split(), the stopwatch continues to run, and the regular stopwatch time can be obtained from the StopWatch object's toString() method. A call to unsplit() releases the split time so that you can call split() again. The stopwatch can be stopped with a call to stop(). If you'd like to start it again, you need to call reset(), which resets the stopwatch. At this point, you can call start() again.

The StopWatchTest class demonstrates several of the features of the StopWatch class.

StopWatchTest.java

package test;

import java.io.IOException;

import org.apache.commons.lang.time.StopWatch;

public class StopWatchTest {

	public static void main(String[] args) throws IOException {

		StopWatch stopWatch = new StopWatch();

		System.out.println("STARTING STOPWATCH");
		stopWatch.start();

		sleep();

		System.out.println("SUSPENDING STOPWATCH");
		stopWatch.suspend();
		System.out.println("Stopwatch time: " + stopWatch);
		sleep();
		System.out.println("Stopwatch time: " + stopWatch + " (doesn't change since suspended)");
		System.out.println("RESUMING STOPWATCH");
		stopWatch.resume();

		sleep();

		System.out.println("Stopwatch time: " + stopWatch);
		System.out.println("SPLITTING STOPWATCH");
		stopWatch.split();
		sleep();
		// Note: stopWatch must be split to call toSplitString()
		System.out.println("Stopwatch split time: " + stopWatch.toSplitString()
				+ " (reported time doesn't change but stopwatch still running)");
		System.out.println("Stopwatch time: " + stopWatch);
		sleep();
		System.out.println("Stopwatch split time: " + stopWatch.toSplitString()
				+ " (reported time doesn't change but stopwatch still running)");
		System.out.println("Stopwatch time: " + stopWatch);
		System.out.println("UNSPLITTING STOPWATCH (removes split effect)");
		stopWatch.unsplit();

		sleep();

		System.out.println("STOPPING STOPWATCH");
		stopWatch.stop();
		System.out.println("Stopwatch time: " + stopWatch);

		System.out.println("RESETTING STOPWATCH");
		stopWatch.reset();
		System.out.println("Stopwatch time: " + stopWatch);

		// Note: stopWatch.start() can now be called since reset() was called.
	}

	public static void sleep() {
		System.out.println("1 second goes by");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

(Continued on page 2)

Page:    1 2 >