Scheduling periodic tasks and clock drift

前端 未结 3 477
耶瑟儿~
耶瑟儿~ 2021-01-23 01:09

I would like to schedule a periodic task which executes every X hours. I have a service which is written in Java and I was thinking of creating a long running background thread

3条回答
  •  猫巷女王i
    2021-01-23 01:39

    If you are worried, write a test process and run it on the target platform. Using the feature you plan to use for the real process (like ScheduledExecutorService), schedule a task to log the host time every 24 hours. If the host doesn't use NTP to keep its clock synchronized, perhaps you could also make call to a time-keeping web service and log that too. After a few days, you should have a good sense of whether you need a method to correct for drift.

    My guess is that the built-in scheduler will be accurate to less than a second per day.

提交回复
热议问题