"Go" in-depth understanding of Java memory Model (v)--lock

Source: Internet
Author: User

Lock Release-Get established happens before relationship

Locks are the most important synchronization mechanism in Java concurrent programming. The lock allows the thread that releases the lock to send a message to the thread that acquires the same lock, except for the exclusion of the critical zone from execution.

Here is the sample code for lock Release-get:

Class Monitorexample {    int a = 0;    Public synchronized void writer () {  //1        a++;                             2    }                                    //3 public    synchronized void Reader () {  //4        int i = A;                       5 ...    }                                    6}

Assume that thread a executes the writer () method, and then thread B executes the reader () method. According to the happens before rule, this process contains a happens before relationship that can be divided into two categories:

    1. According to the Rules of Procedure order, 1 happens before 2, 2 happens before 3; 4 happens before 5, 5 happens before 6.
    2. According to the monitor lock rule, 3 happens before 4.
    3. According to the transitivity of happens before, 2 happens before 5.

The graphical representation of the above happens before relationship is as follows:

In, each arrow links the two nodes that represent a happens before relationship. The black arrows represent the program order rules, the orange arrows represent the monitor lock rules, and the blue arrows indicate the happens before guarantees provided after the rules are combined.

Transfer from:http://www.infoq.com/cn/articles/java-memory-model-5&

"Go" in-depth understanding of Java memory Model (v)--lock

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.