Java: Why can't I stop timer after it is enabled? How to stop Timer

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.