| Author |
Invoke thread at specific time ... ?
|
Vijay S. Rathore
Ranch Hand
Joined: Oct 29, 2001
Posts: 449
|
|
How to invoke a thread daily at a specific time? For example I want to invoke a thread every day at 12.00 A.M. I tried to use public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) from java.util.Timer class. But it needs the second parameter as miliseconds. Any links or suggestions will help.
|
SCJP, SCJD, SCWCD1.4, IBM486, IBM484, IBM 483, IBM 287, IBM141, IBM Certified Enterprise Developer - WebSphere Studio, V5.0
Author of IBM 287 Simulator Exam
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
|
A day is 1000 milliseconds times 60 seconds times 60 minutes times 24 hours; 1000*60*60*24, or 86400000 milliseconds. I generally prefer to write out the arithmetic in the code, as it is perhaps clearer and is less prone to error.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Vijay S. Rathore
Ranch Hand
Joined: Oct 29, 2001
Posts: 449
|
|
That I already got, but how to find out the milliseconds at 12 A.M.? in other words, what should be the output of following program public class TimerThread extends TimerTask { public TimerThread() { super(); } public void run() { System.out.println("Thread is running now ..."); System.out.println("Thread is Stopped now ..."); } public static void main(String [] x){ System.out.println(new Date().getTime()); System.out.println(new Date().getTime() + 2000); (new Timer()).scheduleAtFixedRate(new TimerThread(), (new Date().getTime() + 1000), 2000); } } [ August 11, 2004: Message edited by: Vijay Rathore ]
|
 |
Vijay S. Rathore
Ranch Hand
Joined: Oct 29, 2001
Posts: 449
|
|
Don't worry I got it. The second parameter is the initial invocation of thread. I have to find out the difference of miliseconds at 12 A.M. and current miliseonds and then pass the difference as second parameter. Thanks anyway
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Sometimes asking the question is the best way to see the answer, no? Glad you found that. Timer is very cool, but may be mild overkill in extremely simple situations. You can make your own thread that just sleeps that long, too.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Vijay S. Rathore
Ranch Hand
Joined: Oct 29, 2001
Posts: 449
|
|
|
That's what is the purpose of pair programming.
|
 |
 |
|
|
subject: Invoke thread at specific time ... ?
|
|
|