Tags: control, facing, java, mails, programming, sleep, sleeps, time, wakes
how to control the sleep time exactly
On Java Studio » Java Programming
1,719 words with 4 Comments; publish: Wed, 19 Sep 2007 03:40:00 GMT; (150109.38, « »)
i have a thread that sleeps for 15 minutes and wakes up does some thing( send some mails ) and sleep. the problem i am facing is the thread is not running exactly in 15minutes intervals.
EX:
14:12:06
( no mails sent )
14:27:06
( no mails sent )
14:42:06
( sends some mails )
14:57:29
(no mails )
15:12:29
so that means whenever there is some processing to bo done (sendng mails ) it takes more than 15minutes.
how to control this sleep time to exactly 15 minutes no matter what it does ?
http://java-program.developerfaqs.com/q_java-programming_127424.html
All Comments
Leave a comment...
- 4 Comments

- I don't see your code, but I guess that "sends some mails" took 23 seconds. Adjust your sleep period to take that into account. #1; Thu, 05 Jul 2007 01:59:00 GMT

- You are sleeping for 15 minutes, at the time you call sleep. When it sent some mail, it took time, so now when you sleep, more than 15 minutes would have elapsed. What's the problem with that? #2; Thu, 05 Jul 2007 01:59:00 GMT

If you need exact time to elapse, you have two options:
1. use util.Timer set at fixed intervals;
2. mark current System.millis (I never remember exact terms) when sending starts and then adjust sleeptime every time you are going to send next mail. Here you can find info on this
http://developer.java.sun.com/developer/Books/threads/
Ch. 9 swing counter (note that it does not apply to swing only, it's just technique).
#3; Thu, 05 Jul 2007 01:59:00 GMT

To ensure it starts sending mail (if there is any) every 15 minutes exactly, your thread should simply start another thread that sends the mail. The two operate independently so the length of time it takes to send the mail is irrelevant (unless it takes more than 15 minutes and clashes with another period).
#4; Thu, 05 Jul 2007 01:59:00 GMT