Synchronized keyword (Android)

Source: Internet
Author: User

1. Synchronized Method: the synchronized method is declared 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 has a lock, 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, the lock is released only when it is returned from this method, and then the blocked thread can obtain the lock and re-enter the executable state. 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 you declare a large method 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 put the Code of the member variable of the primary class into a special method and declare it as synchronized, it is called in the main method to solve this problem, but Java provides us with a better solution, that is, the synchronized block.

2. Synchronized block: the synchronized block is declared using the synchronized keyword. Syntax:
synchronized (syncobject) {
// code that allows access control
}< br> the synchronized block is such a code block, 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.
some understanding of synchronized (this)
1. When two concurrent threads access the synchronized (this) synchronization code block in the same object, only one thread can be executed within a time period. The other thread must wait until the current thread finishes executing this code block before executing this code block.
2. However, when one thread accesses a synchronized (this) synchronization code block of the object, the other thread can still access the non-synchronized (this) synchronization code block of the object.
3. When a thread accesses a synchronized (this) synchronization code block of an object, other threads perform this on all other synchronized (this) access to the synchronization code block will be blocked.
4. The third example also applies to other synchronous code blocks. That is to say, when a thread accesses a synchronized (this) synchronization code block of an object, it obtains the object lock of this object. As a result, access by other threads to all the synchronized code parts of the object is temporarily blocked.
5. The above rules apply to other Object locks.

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.