1 23 24 29 42 43 48 49 package com.sun.enterprise.util; 50 51 60 import java.util.logging.Logger ; 62 import java.util.logging.Level ; 63 import com.sun.logging.LogDomains; 64 public class ApproximateClock 66 implements Runnable 67 { 68 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 70 private long sleepTime = 1000; 72 private long now; 73 private Thread timerThread; 74 private long[] time; 75 private int index = 0; 76 private int nextIndex; 77 78 82 public ApproximateClock (long delay) { 83 setDelay(delay); 84 timerThread = new Thread (this, "ApproximateClock"); 85 timerThread.setDaemon(true); 86 time = new long[2]; 87 time[0] = System.currentTimeMillis(); 88 timerThread.start(); 89 } 90 91 95 public long getActualTime() { 96 nextIndex = 1 - index; 97 time[nextIndex] = System.currentTimeMillis(); 98 index = nextIndex; 99 return time[index]; 100 } 101 102 107 public long getTime() { 108 return time[index]; 109 } 110 111 115 public void setDelay(long delay) { 116 sleepTime = (delay < 1) ? 1 : delay; 117 } 118 119 123 public long getDelay() { 124 return sleepTime; 125 } 126 127 130 public final void run() { 131 while (true) { 132 try { 133 getActualTime(); 134 Thread.sleep(sleepTime); 135 } catch (InterruptedException inEx) { 136 break; 137 } 138 } 139 } 140 141 public static void main(String [] args) 142 throws Exception 143 { 144 int count = 100000000; 145 146 long time=0, t1=0, t2 = 0; 147 148 t1 = System.currentTimeMillis(); 149 for (int i=0; i<count; i++) { 150 time = System.currentTimeMillis(); 151 } 152 t2 = System.currentTimeMillis(); 153 _logger.log(Level.FINE,"sys.currentTimeMillis() took: " + ((t2 - t1) / 1000.0) + " seconds " + time); 155 156 t1 = System.currentTimeMillis(); 157 ApproximateClock clock = new ApproximateClock(3000); 158 for (int i=0; i<count; i++) { 159 time = clock.getTime(); 160 } 161 t2 = System.currentTimeMillis(); 162 _logger.log(Level.FINE,"clock.getTime() took: " + ((t2 - t1) / 1000.0) + " seconds " + time); 164 165 } 166 167 } 168 169 | Popular Tags |