tyrex.services
Class Clock

java.lang.Object
  extended byjava.lang.Thread
      extended bytyrex.services.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 currentTimeMillis, and also allows the clock to be artificially advanced for testing purposes.

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

Version:
$Revision: 1.5 $
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, holdsLock, 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.

See Also:
Constant Field Values

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.

See Also:
Constant Field Values
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()

synchronize

public static long synchronize()

main

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


Original code is Copyright (c) 1999-2001, Intalio, Inc. All Rights Reserved. Contributions by MetaBoss team are Copyright (c) 2003-2005, Softaris Pty. Ltd. All Rights Reserved.