• 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

Timer class for an Alarm?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I want to create an alarm application, and after some searching I found that java.util.Timer class may be useful.
But when I checked the Java API, It says 'no real-time guarantees' !!
What does this mean? Does it mean that it will be inaccurate ??
So, is it a good idea to use Timers or there's another alternative ??
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How precise do you require the timer to be?
 
omar elgazzar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:How precise do you require the timer to be?


I want to know the most precise way
 
Saloon Keeper
Posts: 7585
176
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The standard JVM is not realtime capable, so nothing is guaranteed to happen at precise moments in time. Having said that, Timer and TimerTask are very useful classes, and I would be surprised if they deviated even a full second from their intended alarm time.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

omar elgazzar wrote:. . . I want to know the most precise way

So how little an error will you tolerate when the alarm warns you to do something? 1nanosecond? 1μs? 1ms? I think you will find that all contemporary computers' clocks are accurate to the nearest ms (I hope I am right ‍), though 20 years ago you might only have got 0.1s precision.
 
omar elgazzar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've come up with this code [Preliminary Code], what do you think ?

 
Saloon Keeper
Posts: 15510
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few things Omar,

  • Make your class final.
  • Use java.time.LocalTime instead of java.util.Calendar.
  • Use java.util.concurrent.ScheduledExecutorService instead of java.util.Timer.
  • Use java.time.format.DateTimeFormatter instead of String.format().
  •  
    reply
      Bookmark Topic Watch Topic
    • New Topic