Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Timer keeps program from exiting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Timer keeps program from exiting" Watch "Timer keeps program from exiting" New topic
Author

Timer keeps program from exiting

Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137
Hi there,

Once I have created a timer as a member of my class:

Timer mTimer = new Timer(); // data member

my program does not exit after main() completes.

Any one could provide insight on this?
Thanks.

Yan
Ben Buchli
Ranch Hand

Joined: Mar 26, 2004
Posts: 83
do you have a System.exit() call?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
GuaiguaiYan,

In accordance with our naming policy, I ask that you add a space to your display name, or otherwise alter it to comply with the required format FIRST_NAME + SPACE + LAST_NAME.

Concerning your question, I'll assume that you're referring to the java.util.Timer class. In the the java.util.Timer class documentation, note the following contents.
After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the the timer's cancel method.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
your timer runs as a separate thread, which means the program won't exit until the timer terminates.
As timer typically runs in an eternal loop it never terminates unless you tell it specifically that it should by calling its cancel() method.

I suggest you check out the API docs, they're there for a reason.
It clearly states:

After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the the timer's cancel method.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Timer keeps program from exiting
 
Similar Threads
Timer/ TimerTask
help with Java timer
timer
Where did you hear of the ranch ?
Time