File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Invoke thread at specific time ... ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Invoke thread at specific time ... ?" Watch "Invoke thread at specific time ... ?" New topic
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: 24183
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invoke thread at specific time ... ?