1 package org.e2etrace.timer; 2 3 /* 4 * Copyright 2006 Gunther Popp 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /** 20 * Default <code>ITimer</code> implementation. 21 * <p> 22 * 23 * This implementation uses {@link System#currentTimeMillis()} to measure the 24 * duration between the <code>start()</code> and <code>measure()</code> 25 * calls on the timer. The advantage of this approach is that it works with all 26 * JDK releases. The disadvantage is that the accuracy of 27 * <code>currentTimeMillis</code> depends on the underlying operating systems. 28 * On Windows, for example, <code>currentTimeMillis</code> will typically 29 * provide an accuracy of about 10ms, which is too coarse for measuring short 30 * intervals. However, on Linux the accuracy is 1ms, which is exactly what we 31 * want. For details on this issue, please refer to the article "My kingdom for 32 * a good timer!" by Vladimir Roubtsov on JavaWorld (see link below). 33 * <p> 34 * 35 * @author Gunther Popp 36 * @see <a 37 * href="http://www.javaworld.com/javaworld/javaqa/2003-01/01-qa-0110-timing.html">Article 38 * "My kingdom for a good timer!"</a> 39 * 40 */ 41 public class DefaultTimer implements ITimer { 42 43 private long duration = -1; 44 private long start = -1; 45 46 /** 47 * Constructor. New instances can only be created by factories 48 * {@link ITimerFactory} 49 */ 50 DefaultTimer() { 51 52 } 53 54 /** {@inheritDoc} */ 55 public void start() { 56 start = System.currentTimeMillis(); 57 58 } 59 60 /** {@inheritDoc} */ 61 public long measure() { 62 if (start > 0) { 63 duration = System.currentTimeMillis() - start; 64 } 65 66 return duration; 67 68 } 69 70 }