Java Foundation thread synchronized keyword

Source: Internet
Author: User

synchronized keyword, which consists of two usages:synchronized method and synchronized block

1. Synchronized method: Declares the Synchronized method by adding the Synchronized keyword to the method declaration. such as:   public synchronized void accessval (int newval) ;   The Synchronized method Controls access to class member variables: Each class instance corresponds to a lock, and each synchronized method must obtain a lock on the class instance that invokes the method to execute, or the owning thread is blocked. Once the method executes, the lock is exclusive until it is returned from the method, and then the blocked thread can obtain the lock and re-enter the executable state. This mechanism ensures that at the same time for each class instance, at most one of its member functions declared as synchronized is in an executable state (because at most only one of the can obtain the corresponding lock for that class instance), This effectively avoids access violations of class member variables (as long as all methods that may access class member variables are declared as synchronized) .   in Java, not just class instances, each class also corresponds to a lock, so we can also declare a static member function of a class as synchronized to control its access to the static member variable of the class.   The flaw in the Synchronized method: Declaring a large method as synchronized will greatly affect efficiency, typically if the method run () of the thread class is declared as Synchronized, because it has been running throughout the lifetime of the thread, will cause its call to any synchronized method in this class to never succeed. Of course we can to declare it as synchronized by putting the code that accesses the class member variable into a specialized method, and calling it in the main method to solve the problem, but Java provides us with a better solution for . That's the synchronized block.  

2. Synchronized BLOCK: Declare the synchronized block by synchronized keyword. The syntax is as follows: synchronized (syncobject) { //code to allow access control }  The synchronized block is a block of code in which the code must obtain the object SyncObject (as previously described, can be class instances or classes) of the lock can be executed, the specific machinesystem as described above.  Because it can be arbitrary code block, and can arbitrarily specify the locked object, it is more flexible.

Some understanding of synchronized (this)
First, when two concurrent threads access the same object in the synchronized (this) synchronization code block, only one thread can be executed within a single time. Another line

Process must wait for the current thread to execute this block of code before executing the code block.
Second, however, when a thread accesses one synchronized (this) of an object to synchronize a block of code, another thread can still access the non-synchronized in that object

(this) synchronizes the code block.
Third, in particular, when a thread accesses one synchronized (this) of an object to synchronize a block of code, the other thread pairs all other synchronized (this) in the object

Access to the synchronized code block is blocked.
The third example also applies to other synchronous blocks of code. That is, when a thread accesses a synchronized (this) of object to synchronize a block of code, it obtains this

The object lock of the objects. As a result, other threads are temporarily blocking access to all of the synchronization code portions of the object objects

Java Foundation thread synchronized keyword

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.