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?

2 Answers 2

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

1
  • Thank you for the link, but the comment in the linked question already states that it is a naive implementation. Jul 17, 2009 at 11:46
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

1
  • 1
    Timer and TimerTask are pretty much obsolete now. ScheduledExecutorService does all that and more.
    – skaffman
    Jul 17, 2009 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.