1

we need to keep an eye on the pc clock from a Java program. For this, we schedule a Runnable using scheduleAtFixedRate() every 500 ms. We call System.currentTimeMillis() from this every time. If we see that the there is a bigger difference then 500 ms +- a certain allowed delta, then we assume the clock has changed (then we need to do some other stuff).

Would this be a correct way of doing things? Tests on Linux show that a 50 ms delta is enough during normal operation. On windows, we have to increase it too 100 ms, otherwise, it is thinking the time has changed on every check we do.

Any other ideas on how to do this?

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211

2 Answers2

2

if the objective is to detect clock changes, you may test the current clock against a know timeserver reference

look for more info here Java NTP client http://support.ntp.org/bin/view/Support/JavaSntpClient using an absolute time source you'll be able to detect clock changes

Community
  • 1
  • 1
Lorenzo Boccaccia
  • 6,041
  • 2
  • 19
  • 29
0

I assume you use the classes Timer and TimerTask.

May be you could use the method schedule instead of scheduleAtFixedRate. The method schedule passing a Date, allows you to set the task for a Date of execution, you could just keep rescheduling at the end of your last execution. You can also use schedule passing the delay, which does not perform catch up as scheduleAtFixedRate does.

javadoc Timer

fgui
  • 1,564
  • 1
  • 13
  • 14
  • 1
    Timer and TimerTask are pretty much obsolete now. ScheduledExecutorService does all that and more. – skaffman Jul 17 '09 at 11:07