Java Reference
In-Depth Information
ch14/selsort/StopWatch.java
1 /**
2 A stopwatch accumulates time when it is running. You can
3 repeatedly start and stop the stopwatch. You can use a
4 stopwatch to measure the running time of a program.
5 */
6 public class StopWatch
7 {
8 /**
9 Constructs a stopwatch that is in the stopped state
10 and has no time accumulated.
11 */
12 public StopWatch()
13 {
14 reset();
15 }
16
17 /**
18 Starts the stopwatch. Time starts accumulating now.
19 */
20 public void start()
21 {
22 if (isRunning) return ;
23 isRunning = true ;
24 startTime = System.currentTimeMillis();
25 }
26
27 /**
28 Stops the stopwatch. Time stops accumulating and is
29 is added to the elapsed time.
30 */
31 public void stop()
32 {
33 if (!isRunning) return ;
34 isRunning = false ;
35 long endTime =
System.currentTimeMillis();
36 elapsedTime = elapsedTime + endTime -
startTime;
37 }
Search WWH ::




Custom Search