multithreading - Java Thread.sleep waking up "much" earlier than the scheduled time -
i have requirement of writing daemon initiates number of threads wakes @ different times. daemon written in java using commons apache library , run on linux machine (fedora 13 ). 1 thread wakes everyday perform task happens scheduled. there thread scheduled wake every monday @ 6 perform task not happen scheduled. problem thread wakes before actual scheduled time. runs 2 days after completion of previous run though should run after week. waiting time calculated correctly using our own timer class , because reuses existing code not see problem in this.
what problem here?
thanks
thread.sleep() doesn't make guarantees, , might wake earlier expected. should use in loop:
long curtime = system.currenttimemillis(); while (wakeuptime - curtime > 0) { try { thread.sleep(wakeuptime - curtime); } catch (interruptedexception ex) { } curtime = system.currenttimemillis(); }
Comments
Post a Comment