Java synchronization mechanism: sychronized to Code impact

Source: Internet
Author: User

Java support for multithreading and synchronization mechanism by popular, it seems that the use of the Synchronized keyword can easily solve the problem of multithreading shared data synchronization. What's going on? --also have to synchronized the role of the key words to understand the conclusion.

In general, the Synchronized keyword can be used as a modifier of a function, or as a statement within a function, which is usually a synchronized method and a block of synchronized statements. In the finer categories, synchronized can be used for instance variables, object reference (reference), static functions, and class literals (class name literal constants).

Before we further elaborate, we need to be clear on a few points:

A Whether the Synchronized keyword is added to a method or an object, the lock it obtains is an object rather than a piece of code or function as a lock-and the synchronization method is likely to be accessed by objects of other threads.

B Each object has only one lock (lock) associated with it.

C To achieve synchronization is a very large system overhead as a cost, and may even cause deadlock, so try to avoid unnecessary synchronization control.

Next, discuss synchronized's impact on the code in different places:

Assuming that P1, P2 are different objects of the same class, this class defines synchronization blocks or synchronization methods that can be invoked by P1, P2, and so on.

1. When you treat synchronized as a function modifier, the sample code is as follows:

Public synchronized void methodAAA()
{
//….
}

This is also the synchronization method, then synchronized locked is what object? It locks a call to this synchronization method object. That is, when an object P1 the synchronization method in a different thread, they form a mutex that achieves the effect of synchronization. But another object generated by the class that this object belongs to P2 can invoke the method that was added to the Synchronized keyword arbitrarily.

The example code above is equivalent to the following code:

public void methodAAA()
{
synchronized (this)   // (1)
{
    //…..
}
}

(1) What does this refer to? It refers to the object that invokes this method, such as P1. The visible synchronization method is essentially a synchronized action on the object reference. -The thread that gets the lock on the P1 object can invoke the P1 synchronization method, and for P2, P1 This lock has nothing to do with it, and the program may get rid of the synchronization mechanism in this situation, resulting in data chaos.

2. Sync block, sample code as follows:

public void method3(SomeObject so)
{
synchronized(so)
{
   //…..
}
}

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.