Java High concurrency synchronized

Source: Internet
Author: User

Java High concurrency synchronized

Synchronized can guarantee that the code block or method at run time, only one method can enter the critical area at the same time, also can guarantee the memory visibility of the shared variable.

Synchronized can be used in three situations:

1. Normal synchronization method, lock is the current instance object

2. Static synchronization method, lock is the class object of the current classes

3. Synchronization method block, lock is the object inside the parentheses

 Package Com.tianmaying.crawler.impl;  Public class synchronizedtest {    publicsynchronizedvoid  test1 () {}}
public void Test2 () {
Synchronized (this) {}
}
public static synchronized void Test3 () {}

To decompile a project using Javap-c synchronizedtest

It can be seen that:

Synchronous methods, synchronous static methods: All are labeled the method is synchronized, and there is no special label, but according to the article "http://www.cnblogs.com/javaminer/p/3889023.html", means that the method of access The synchronized flag position for the _flags field is 1

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ------------------------

Two concepts: Java object header, monitor

Java High concurrency 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.