Some problems with shutting down threads

Source: Internet
Author: User

Thread.stop method, although it does stop a running thread, however, this method is unsafe and is not advocated, and the Stop method should not be used to close the Java thread.

The correct method should be to set the volatile Boolean stop = False, so that all threads can detect a change in the stop value in a timely manner, the thread runs during a periodic or critical point check of the stop value, and when the stop is found to be true by another thread, the thread's run is terminated.

But the above method also has the imperfect place, when the thread waits, sleep and so on the case will not be able to judge the value of stop, that is to say the thread may always block down. So you want to close the thread, in addition to checking the stop value for periodic or critical points, and calling interrupt to break the blocking state of the thread.

Even if you do the above, the thread may not terminate immediately, waiting for the end of the operating system to be dispatched.

Executorservice's shutdown is to slow down the thread pool, unable to join the new task, Shutdownnow want to close the thread pool immediately, Shutdownnow call interrupt, but call interrupt, is not immediately closed.

Executorservice does not call shutdown, the thread pool is closed 1 minutes after all tasks have been completed.

  

  

Some problems with shutting down threads

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.