Java synchronized keyword usage

Source: Internet
Author: User
In the case of multithreading, multiple threads of the same process share the same storage space, which brings convenience while also causing access conflicts. Java provides a dedicated mechanism to resolve such conflicts,
This effectively prevents the same data object from being simultaneously accessed by multiple threads. Because we can use private keywords to ensure that data objects can only be accessed by methods, we only need to propose a mechanism for methods,
This mechanism is the synchronized keyword, which includes two usage methods: Synchronized Method and synchronized block.
1. Synchronized Method: declare the Synchronized Method by adding the synchronized keyword to the method declaration. For example:
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 the lock of the class instance that calls the method before execution, otherwise the thread is blocked,
Once the method is executed, the lock is exclusive until the lock is released when the method is returned. Then, the blocked thread can obtain the lock and re-enter the executable status. This mechanism ensures that, at the same time,
Only one of all the member functions declared as synchronized is in the executable state (because at most only one member function can obtain the lock corresponding to this type of instance ),
This effectively avoids access conflicts between class member variables (as long as all methods that may be used to invoke class member variables are declared as synchronized ).
In Java, not only are class instances, but each class also corresponds to a lock. In this way, we can declare the static member function of the class as synchronized, to control its access to static member variables of the class.
Synchronized: if a large method is declared as synchronized, the efficiency will be greatly affected. Typically, if you declare the thread-class method run () as synchronized,
Since the thread is always running throughout its life cycle, it will never succeed in calling any Synchronized Method of this class. Of course, we can
Code Put it in a special method, declare it as synchronized, and call it in the main method to solve this problem, but Java provides us with a better solution,
That is the synchronized block.
2. Synchronized block: Use the synchronized keyword to declare the synchronized block. Syntax:
Synchronized (syncobject ){
// Code that allows access control
}
The synchronized block is a code block in which the Code must obtain the lock of the object syncobject (as described earlier, it can be a class instance or class) before execution. The specific mechanism is described earlier.
It is highly flexible because it can target any code block and can specify any locked object.
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.