[concurrency-interest] ScheduledThreadPoolExecutor schdeuling problem ?

Hanson Char hanson.char at gmail.com
Wed Jan 2 05:06:27 EST 2008


For scheduling purposes, I wonder if it would make sense to
instantiate a clock (see below) that can provide consistent
currentTimeMilli based on nano time, and pass the clock instance
around.  Places that need to invoke System.currentTimeMilli (for
scheduling purposes) would then instead invoke the clock's
currentTimeMilli to obtain a consistent view of time.  This clock's
currentTimeMilli would be immune to the out-of-date refresh problem of
System.currentTimeMilli.  However, since the System.nanoTime could be
implemented on a different hardware device than
System.currentTimeMilli, such clock would probably gradually diverge
from the System.currentTimeMilli with no upper bound :-(

Hanson Char

import static java.util.concurrent.TimeUnit.NANOSECONDS;
import net.jcip.annotations.Immutable;

@Immutable
public class Clock {
	private final long startNano = System.nanoTime();
	private final long startMilli = System.currentTimeMillis();
	
	public long currentTimeMilli() {
		return startMilli + NANOSECONDS.toMillis(System.nanoTime() - startNano);
	}
}


More information about the Concurrency-interest mailing list