| Author |
sleep until a date occurs
|
Murad Iqbal
Ranch Hand
Joined: Dec 09, 2003
Posts: 90
|
|
Hi all! How can I make my thread sleep until a specified date (including hours, minutes and millis) occurs? Or do I have to calculate the current milliseconds and subtract it from the milliseconds of that date and then pass it on to thread.sleep method? A quick reply and help is higly commended
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
There's no "sleep until date" function, so your two alternatives would be to compute a sleep interval (easy to do using the Date class) or poll like
|
[Jess in Action][AskingGoodQuestions]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Also take a look at java.util.Timer schedule(TimerTask task, Date time). Let us know if that does what you need.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Abdul Basit
Greenhorn
Joined: Jan 25, 2002
Posts: 27
|
|
|
murad i think u have a fit solution in your mind , calculate the second and pass it to sleep method , let us know your experience
|
 |
Murad Iqbal
Ranch Hand
Joined: Dec 09, 2003
Posts: 90
|
|
Well, the problem is solved. What I have done is that I have created two calendars. First calendar has the current time on my machine and will return the current time in millis when i call the getTimeInMillis over it. The second calendar's DAY_OF_WEEK, HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND are set to the desired date when I want the thread to sleep. I then invoked getTimeInMillis on the second calendar to get that time in milliseconds. Thereafter, I subtracted the milliseconds I got from the first calendar from the second calendar's millis. So now I have the milliseconds I want my thread to sleep. I then invoke Thread.sleep(milliseconds) to serve my purpose. Regards, Muhammad Murad Iqbal
|
 |
R Jelda
Greenhorn
Joined: May 13, 2002
Posts: 26
|
|
Hi, You don't need the first Calender. U could just use System.currentTimeMillis(). Regards, Jelda
|
"If you have an apple and I have an apple and we exchange apples then you and I will still each have one apple. But if you have an idea and I have an idea and we exchange these ideas, then each of us will have two ideas." -George Bernard Shaw
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Cool, Murad. I think that's exactly what Timer does inside. Timer might pay off if you had a LOT of things to trigger at various times because it will manage any number of timer events on a single thread. It probably just sorts them in target time order and sleeps until the next one is due.
|
 |
JunKao Lin
Greenhorn
Joined: Feb 05, 2004
Posts: 10
|
|
Hi Murad,
Originally posted by Murad Iqbal: Hi all! How can I make my thread sleep until a specified date (including hours, minutes and millis) occurs? Or do I have to calculate the current milliseconds and subtract it from the milliseconds of that date and then pass it on to thread.sleep method? A quick reply and help is higly commended
I think you'd better use the data.wait() and data.notify().When the specified date occurs,you could invoke the data.notify() to make the method which has invoked the data.wait() become runnable again.
|
ljk
|
 |
 |
|
|
subject: sleep until a date occurs
|
|
|