I also encountered this problem. Okay, I will repost it:
Timer and timertask can be used to regularly complete certain tasks. However, a strange phenomenon occurs during the running process: All timertasks have been completed.ProgramIt should be automatically exited, but it does not! I thought it was an eclipse bug, and then try again in cmd, it is also impossible to exit. This phenomenon occurs when a new timer () is created. It's strange that Google cannot find the answer, but finds a post with just a few sporadic questions. Let's take a look at the help documentation... In the Timer class of the jdk1.5 document, there is a saying: "After the final reference to the timer object is complete and all the unprocessed tasks have been completed, the task execution thread of the timer terminates normally (and becomes the garbage collection object ). However, this may take a long time ." But how long is this "long time? Wait until it stops... Since it will become the object of garbage collection, let's take the initiative to let the system recycle it. Add a line at the end of the run () method of each timertaskCode: System. GC (); then run the program. It is natural that GC () should be called at the end of the last timertask of timer. However, it is found that GC () has been called in a timertask (), then the program ends normally. And GC () does not have to be placed at the end.
Http://www.douban.com/note/64661564/