01: package com.jamonapi;
02:
03: /**
04: * <p>Monitor that tracks execution time in milliseconds. </p>
05: *
06: * <p>Note due to the fact that when start is called it resets the startTime instance
07: * variable and different threads can call start() before one of the threads calls
08: * stop this object when used BY ITSELF would not be thread safe. However, when
09: * not reused i.e. when TimeMon's are always taken from MonitorFactory it is threadsafe.</p>
10: *
11: * <p>I didn't attempt to make this thread safe as even if it was having two threads
12: * subsequently call start, start before a stop would reset the startTime and so
13: * make one of the callers time off.</p>
14: *
15: * <p>Note this class is a thin wrapper that adds time capabilities to the basic Monitor
16: * class </p>
17: */
18:
19: class TimeMon extends DecoMon {
20:
21: private long startTime;
22:
23: public TimeMon(MonKey key, MonInternals monData) {
24: super (key, monData);
25: }
26:
27: public Monitor start() {
28: super .start();
29: startTime = System.currentTimeMillis();
30: return this ;
31: }
32:
33: // note synchronization is handled by the underlying object ?????
34: public Monitor stop() {
35: long endTime = System.currentTimeMillis();
36: setAccessStats(endTime);// saves some cycles by not recalculating time for time monitors
37: add(endTime - startTime);// accrue status
38: super .stop();// decrement active.
39: return this ;
40: }
41:
42: public void reset() {
43: super .reset();
44: startTime = 0;
45: }
46:
47: }
|