• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sleep until a date occurs

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic