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 questions java.util.Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "questions java.util.Timer" Watch "questions java.util.Timer" New topic
Author

questions java.util.Timer

Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137
Hi there,
I have two questions on Java Timer.
Say, I use a Timer to schedule several recurring TimerTasks (e.g., they
execute every a few hours, etc.). Since by default Timer runs as a user
thread, would that keep my application from terminating?
The JavaDoc on Timer says that only after all the scheduled tasks complete
(what defines "Complete" for recurring TimerTasks, would they ever
complete??), the Timer's thread would then terminate. So, if I call
System.exit(0) from my code, would my application exit immediately or does
it has to wait till all timer tasks complete?
The second question is that in addition to these TimerTasks, I also want to
schedule another TimerTask, whose execution requires me to exit the
application and restart it, and this task can only run if no other TimerTask
is running. My question is, how would I know that no TimerTask is running at
any given moment?
Thanks.
Yan
Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137
I think I found the answer to the 2nd question, only.
Since I create all the TimerTask objects using the same Timer, they are executed one at a time, with only one runs at any given time. So, there is no need for checking no other TimerTask is running, because there will be none.
Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137
Looks like System.exit(0) terminates the application without waiting for threads to finish. Is that right?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34


Looks like System.exit(0) terminates the application without waiting for threads to finish. Is that right?

Yep.


[Jess in Action][AskingGoodQuestions]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: questions java.util.Timer