This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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
Joined: Dec 09, 2003
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
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
Joined: Jan 29, 2003
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.
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.