Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer keeps program from exiting

 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do you have a System.exit() call?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic