Why is the Stop () and Suspend () methods in Java threads not recommended?

Source: Internet
Author: User

Using the Synchronized keyword to modify the synchronization method;

Against the use of Stop () is because it is unsafe. It unlocks all locks acquired by the thread, and

And if the object is in an incoherent state, then other threads can check and modify it in that State

Children The result is difficult to check out the real problem;

The suspend () method is prone to deadlocks. When calling suspend (), the target line routines to stop,

But still hold the lock that was acquired before that. At this point, no other thread can access the locked resource.

Unless the thread that is "suspended" resumes running. For any thread to say, if they want to restore the target thread,

Attempting to use any of the locked resources at the same time will cause a deadlock. Therefore, suspend () should not be used,

Instead, place a flag in your thread class that indicates whether the thread should be active or suspended. If the logo

Indicates that the thread should hang, and wait () is used to enter the wait state. If the flag indicates that the thread should be restored,

The thread is restarted with a notify ().

Why is the Stop () and Suspend () methods in Java threads not recommended?

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.