org.exolab.jms.common.uuid
Class Clock

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--org.exolab.jms.common.uuid.Clock
All Implemented Interfaces:
java.lang.Runnable

public final class Clock
extends java.lang.Thread

Provides an efficient mechanism for obtaining the current system time. Uses a background thread to automatically increment an internal clock and periodically synchronize with the system clock. The method clock is more efficient than java.lang.System.currentTimeMillis, and also allows the clock to be artificially advanced for testing purposes.

The clock is thread-safe and consumes a single thread.

This class originally came from Tyrex: http://tyrex.sourceforge.net

Version:
$Revision: 1.2 $
Author:
Assaf Arkin

Field Summary
static int SYNCH_EVERY
          The number of unsychronized cycles before the clock is synchronized with the system clock.
static int UNSYNCH_TICKS
          The number of clock ticks in each unsynchronized cycle.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Method Summary
static void advance(long byMillis)
          Artficially advances the clock.
static long clock()
          Returns the current clock.
static long getAdvance()
          Returns the number of milliseconds by which the clock is advanced.
static int getUnsynchTicks()
          Returns the number of clock ticks in each unsynchronized cycle.
static void main(java.lang.String[] args)
           
 void run()
           
static void setSynchEvery(int every)
          Sets the number of unsynchronized cycles before the clock is synchronized with the system clock.
static void setUnsynchTicks(int ticks)
          Sets the number of clock ticks in each unsynchronized cycle.
static long synchronize()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNSYNCH_TICKS

public static final int UNSYNCH_TICKS
The number of clock ticks in each unsynchronized cycle. The default is 100 milliseconds.

SYNCH_EVERY

public static final int SYNCH_EVERY
The number of unsychronized cycles before the clock is synchronized with the system clock. The default is 10.
Method Detail

clock

public static long clock()
Returns the current clock.
Returns:
The current clock

setUnsynchTicks

public static void setUnsynchTicks(int ticks)
Sets the number of clock ticks in each unsynchronized cycle. Use zero to restore the default value.

The internal clock is advanced every cycle, the length of the cycle is controlled by this property. A higher value results in a lower clock resolution.

Parameters:
ticks - The number of clock ticks (milliseconds) for each unsynchronized cycle

getUnsynchTicks

public static int getUnsynchTicks()
Returns the number of clock ticks in each unsynchronized cycle.
Returns:
The number of clock ticks (milliseconds) for each unsynchronized cycle

setSynchEvery

public static void setSynchEvery(int every)
Sets the number of unsynchronized cycles before the clock is synchronized with the system clock.

Synchronization will occur every unsynchTicks * synchEvery milliseconds. The larger the value, the less accurate the clock is.

Parameters:
every - The number of unsynchronized cycles

advance

public static void advance(long byMillis)
Artficially advances the clock.
Parameters:
byMillis - The number of milliseconds by which to advance the clock (must be positive)

getAdvance

public static long getAdvance()
Returns the number of milliseconds by which the clock is advanced.
Returns:
The number of milliseconds by which the clock is advanced

run

public void run()
Overrides:
run in class java.lang.Thread

synchronize

public static long synchronize()

main

public static void main(java.lang.String[] args)


Copyright © 1999-2007 The OpenJMS Group. All Rights Reserved.