Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if system time has been moved backwards in java, or time proof timers

Tags:

java

timer

I have a box thats using ntp and we have several java programs running on them that display a clock. The problem we are having is if anything kicks the system time backwards all our timers that do things like animate the clock stop and wait until the system time catches back up to where it was. I need to find a way to either detect when the system time has been changed backwards and reset all our timers or a set of timers that can schedule repeatedly but still be proof against the clock changing.

As a note I already tried the quartz timer package it has the same problem as the regular java timers.

like image 527
tharris Avatar asked Aug 12 '10 16:08

tharris


1 Answers

Pretty much all timers set a future time and then regularly compare current time to the given time. That's why timers "stall" when real time goes backward.

Unfortunately, ALL of the timers in the JVM are related to the time of day. For example, java.util.Timer does a Object.wait(milliSeconds) for an event to fire. That boils down to a thread call that also does a wait, for t milliseconds. And it's always relative to the "time of day".

So, basically there's no real way to do this in java without a spinning, CPU sucking loop waiting for the time to go backward in order to inform the timers you care about to reset.

like image 75
Will Hartung Avatar answered Sep 22 '22 19:09

Will Hartung