File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sleep until a date occurs

 
Murad Iqbal
Ranch Hand
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also take a look at java.util.Timer schedule(TimerTask task, Date time). Let us know if that does what you need.
 
Abdul Basit
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You don't need the first Calender.
U could just use System.currentTimeMillis().
Regards,
Jelda
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic