Interrupt in java

hercules

Disciple
Hi,
I wanted to write a simple program which would trigger at particular system time.Say for example an alram, which would fire at exactly 6:00 Am system time.
What can be the best way of doing this in core java ? Sleeping in a thread and keep polling for system time doesn't sound a good idea. Any interrupt mechanism ?
 
are you sure you want to run this in java???

what are you trying to achieve?

if you just want a functionality/job to trigger at a point of time in windows, the you can use the windows task scheduler and get your job done. here you can write a simple .bat file which the task scheduler can trigger.

if you can explain what your goal is, then we can provide better solutions.

writing a program in java, means you will need to have the JRE on the target pc. I think you will need to keep a thread running for polling.

Instead of interrupt mechanism, one logic i can suggest is,

1) write the wake up time in the app.properties file.

2)let the thread read the alarm time from the file

3) calculate the time diff between the current time and your target time and set this time diff as the sleep period for your thread.

in this way you wont need to keep polling after every x interval.

a word of caution on thread.sleep here: Thread.sleep
 
I dont suppose so. You can create a thread and keep running it parallel to your program. The processing in that thread can check for time and do the necessary function (sound alarm etc). Based on that you can either pause all your existing threads or keep them running.

@mhamirma, the alarm was just an example and might not be his actual requirement.
 
Yes, actually I already have a java application where i want to implement an alarm. I did something similar to what mhamirma suggested. Calculated the time diff from now till alarm time, and scheduled a timer.
I was just wondering if this itself is the most efficient way of doing it . I guess may be Yes!

Thanks
 
Back
Top