FileDocCategorySizeDatePackage
ApproximateClock.javaAPI DocGlassfish v2 API6250Fri May 04 22:32:06 BST 2007com.sun.enterprise.util

ApproximateClock

public class ApproximateClock extends Object implements Runnable
ApproximateClock returns the approximate time instead of calling (the expensive) System.currentTimeMillis(). The method getTime() returns the approximate time. The correctness of the time depends on the delay that is passed in the constructor. A background thread updates the time every 'delay' milliseconds. The exact time can still be obtained by using the getActualTime() method.

Fields Summary
static Logger
_logger
private long
sleepTime
private long
now
private Thread
timerThread
private long[]
time
private int
index
private int
nextIndex
Constructors Summary
public ApproximateClock(long delay)
Creates an ApproximateClock.

param
delay The time interval to update time

	
	           	 
	    
		setDelay(delay);
		timerThread = new Thread(this, "ApproximateClock");
		timerThread.setDaemon(true);
		time = new long[2];
		time[0] = System.currentTimeMillis();
		timerThread.start();
	
Methods Summary
public longgetActualTime()
Returns the actual/correct time. Same as calling System.currentTimeMillis()

return
The current time (Same as System.currentTimeMillis())

		nextIndex = 1 - index;
		time[nextIndex] = System.currentTimeMillis();
		index = nextIndex;
		return time[index];
    
public longgetDelay()
Returns the delay

return
The delay (in milliseconds) between updates.

    	return sleepTime;
    
public longgetTime()
Returns the approximate time. Returns the time that was updated (utmost) 'delay' milliseconds earlier.

return
The approximate time

    	return time[index];
    
public static voidmain(java.lang.String[] args)

    	int count = 100000000;
    	
    	long time=0, t1=0, t2 = 0;
    	    	
    	t1 = System.currentTimeMillis();
    	for (int i=0; i<count; i++) {
    		time = System.currentTimeMillis();
    	}
    	t2 = System.currentTimeMillis();
//Bug    	System.out.println("sys.currentTimeMillis() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
	_logger.log(Level.FINE,"sys.currentTimeMillis() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
    	
    	t1 = System.currentTimeMillis();
    	ApproximateClock clock = new ApproximateClock(3000);
    	for (int i=0; i<count; i++) {
    		time = clock.getTime();
    	}
    	t2 = System.currentTimeMillis();
//Bug    	System.out.println("clcok.getTime() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
	_logger.log(Level.FINE,"clock.getTime() took: " + ((t2 - t1) / 1000.0) + " seconds " + time);
    	
    
public final voidrun()
The thread's run method to update the time periodically

    	while (true) {
    		try {
    			getActualTime();
    			Thread.sleep(sleepTime);
    		} catch (InterruptedException inEx) {
    			break;
    		}
    	}
    
public voidsetDelay(long delay)
Set the delay for updating the time.

param
The delay (in milliseconds) between updates.

    	sleepTime = (delay < 1) ? 1 : delay;