1 45 package org.exolab.jms.common.uuid; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 51 66 public final class Clock extends Thread { 67 68 72 public static final int UNSYNCH_TICKS = 100; 73 74 75 79 public static final int SYNCH_EVERY = 10; 80 81 82 85 private static long _clock; 86 87 88 92 private static int _unsynchTicks = UNSYNCH_TICKS; 93 94 98 private static int _synchEvery = SYNCH_EVERY; 99 100 101 105 private static long _advance; 106 107 108 113 private static int _adjust; 114 115 118 private static final Log _log = LogFactory.getLog(Clock.class); 119 120 121 126 public static synchronized long clock() { 127 return _clock; 129 } 130 131 132 143 public static void setUnsynchTicks(int ticks) { 144 if (ticks <= 0) { 145 ticks = UNSYNCH_TICKS; 146 } else if (ticks < 100) { 147 ticks = 100; 148 } 149 _unsynchTicks = ticks; 150 } 151 152 153 159 public static int getUnsynchTicks() { 160 return _unsynchTicks; 161 } 162 163 164 174 public static void setSynchEvery(int every) { 175 if (every <= 0) 176 every = SYNCH_EVERY; 177 _synchEvery = every; 178 } 179 180 181 187 public synchronized static void advance(long byMillis) { 188 _advance += byMillis; 190 _clock += byMillis; 191 } 192 193 194 201 public static long getAdvance() { 202 return _advance; 203 } 204 205 206 public void run() { 207 while (true) { 208 try { 209 for (int i = 0; i < _synchEvery; ++i) { 210 sleep(_unsynchTicks); 211 synchronized (Clock.class) { 212 _clock += _unsynchTicks + _adjust; 213 } 214 } 215 synchronize(); 216 } catch (InterruptedException exception) { 217 synchronize(); 219 } catch (ThreadDeath exception) { 220 _log.debug("clock daemon terminating", exception); 223 throw exception; 224 } catch (Throwable exception) { 225 _log.error("Internal error in clock daemon", exception); 226 } 227 } 228 } 229 230 231 public static synchronized long synchronize() { 232 long current; 233 long retarded; 234 long clock; 235 int adjust; 236 237 current = System.currentTimeMillis(); 238 clock = _clock; 239 retarded = clock - _advance; 240 if (current != retarded) { 242 adjust = (int) (current - retarded) / _synchEvery; 243 if (adjust != 0) { 244 _adjust += adjust; 245 250 } 251 } 252 if (current > retarded) { 254 clock = current + _advance; 255 _clock = clock; 256 } 257 return clock; 258 } 259 260 261 private Clock() { 262 super("Clock Daemon"); 263 _clock = System.currentTimeMillis(); 264 setPriority(Thread.MAX_PRIORITY); 265 setDaemon(true); 266 start(); 267 } 268 269 static { 270 new Clock(); 271 } 272 273 public static void main(String [] args) { 274 long clock; 275 int count; 276 277 try { 278 count = 1000000; 279 System.out.println("Using Clock.clock()"); 280 clock = System.currentTimeMillis(); 281 for (int i = 0; i < count; ++i) { 282 if ((i % 100) == 0) 283 synchronize(); 284 else 285 clock(); 286 } 287 clock = System.currentTimeMillis() - clock; 288 System.out.println("Performed " + count + " in " + clock + "ms"); 289 System.out.println("Using System.currentTimeMillis()"); 290 clock = System.currentTimeMillis(); 291 for (int i = 0; i < count; ++i) 292 System.currentTimeMillis(); 293 clock = System.currentTimeMillis() - clock; 294 System.out.println("Performed " + count + " in " + clock + "ms"); 295 } catch (Exception except) { 296 } 297 } 298 299 } 300 | Popular Tags |