The true meaning of synchronized

Source: Internet
Author: User

@synchronized lock is always the object, the value of the object, can only lock the object, constants and so on can not be added synchronized, once compiled will not pass

@synchronized the non-static in the lock object is the object that the lock calls the method, the entire object

The static method in @synchronized lock object is to lock the entire class, and synchronized (X.class) and the lock static code block are all the same as the lock class loader to the object of this class, remember that this class object is not our own new class object, and Is the object that the JVM uses to load this class, and 2 is not the same.

@synchronized lock a property in an object, this property must also be an object, not a constant, such as a collection, and the synchronized (set) has nothing to do with this object, because the set is in this object, but synchronized The object is a locked object, that is, the collection object, so it is not related to the large object where the property is located, that is, when a small object in a large object is locked, the other properties or methods in that large object are accessible, not affected by that lock small object.

The true meaning of synchronized

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.