tyrex.services

Class Clock

public final class Clock extends 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 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 intSYNCH_EVERY
The number of unsychronized cycles before the clock is synchronized with the system clock.
static intUNSYNCH_TICKS
The number of clock ticks in each unsynchronized cycle.
Method Summary
static voidadvance(long byMillis)
Artficially advances the clock.
static longclock()
Returns the current clock.
static longgetAdvance()
Returns the number of milliseconds by which the clock is advanced.
static intgetUnsynchTicks()
Returns the number of clock ticks in each unsynchronized cycle.
static voidmain(String[] args)
voidrun()
static voidsetSynchEvery(int every)
Sets the number of unsynchronized cycles before the clock is synchronized with the system clock.
static voidsetUnsynchTicks(int ticks)
Sets the number of clock ticks in each unsynchronized cycle.
static longsynchronize()

Field Detail

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.

UNSYNCH_TICKS

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

Method Detail

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)

clock

public static long clock()
Returns the current clock.

Returns: The current clock

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

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

main

public static void main(String[] args)

run

public void run()

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

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

synchronize

public static long synchronize()
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.