Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

questions java.util.Timer

 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like System.exit(0) terminates the application without waiting for threads to finish. Is that right?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Yep.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic