There are several methods in Java to implement a thread. What keywords are used to modify the synchronization method? Why is the stop () and suspend () methods not recommended?

Source: Internet
Author: User

Two methods:

Method 1: Inherit from Thread class
Method 2: implement the runnable interface

 

Keywords: Synchronized

 

Each object has an exclusive lock. The exclusive lock only limits the access of the thread to its synchronization method. It does not make sense to the exclusive lock for non-synchronous methods.
Synchronized is a method to solve synchronization problems in Java. Synchronized can be used to modify the method. This method is the synchronous method. An object can have any number of Synchronization Methods. Only one thread can access these Synchronization Methods at any time. For example, thread T1 is accessing the synchronization method M1, and there is another synchronization method m2 that is not accessed, but since there is already a thread that is accessing the synchronization method M1 of this object, so the thread trying to access m2 will be blocked. The access to M1 is complete.
It can be seen that the synchronous method ensures that only one thread can enter the synchronization method during its runtime. Once the method ends, the thread in it will lose its exclusive right to the object.
The synchronized block specifies the exclusive permission of the object to be obtained. Once obtained, the thread will always master the exclusive permission of the object during the execution of the block. In this case, it can continuously access the Synchronization Methods of multiple objects. In the whole process, the exclusive permission is firmly in the hands of this thread, and other threads have no chance. If no synchronization statement block exists, if the synchronous method of an object is continuously accessed, the previous method will be returned to the gap between the next method call, other threads have the opportunity to obtain the exclusive permission of this object first.

 

The use of stop () is opposed because it is insecure.It unlocks all the locks obtained by the thread. When the stop () method is called on a thread object, the thread running the thread object stops immediately, assume that a thread is executing: Synchronized void {x = 3; y = 4;} because the method is synchronous, X and Y are always assigned values when multiple threads access it, if a thread is running to X = 3;, the STOP () method is called. Even in the synchronization block, it simply stops.Incomplete disability data. The most basic condition in multi-threaded programming must ensure data integrity. Therefore, please forget the stop method of the thread. In the future, we will never say "stop the thread" again. And if the object is in an incoherent State, other threads can check and modify them in that state. It is difficult to check the real problem.

The suspend () method is prone to deadlocks.When suspend () is called, the target thread stops, but still holds the lock obtained before that. At this time, no other thread can access the locked resources, unless it is "suspended" the thread resumes running. For any thread, if they want to restore the target thread and attempt to use any locked resource, it will cause a deadlock. Therefore, you should not use suspend (), but set a flag in your Thread class to indicate whether the thread should be active or suspended. If the flag indicates that the thread should be suspended, use wait () to enter the waiting state. If the flag indicates that the thread should be restored, use a notify () Restart the thread.

 

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.