Java memory model

Source: Internet
Author: User
Tags flushes

Two important issues in concurrent programming:

1. How to communicate between threads (shared memory, message delivery);

2. How to synchronize between threads;

In the concurrency model of shared memory , the common state of the program is shared between threads, and the threads communicate implicitly through the public state in the write-read memory;

in in the concurrency model of message passing , there is no public state between threads, and the threads must communicate explicitly by sending messages explicitly.

synchronization refers to the mechanism by which a program controls the relative order of operations between different threads. In the shared-memory concurrency model, synchronization is performed explicitly. The programmer must explicitly specify that a method or piece of code requires mutually exclusive execution between threads, and in the concurrency model of the message delivery, the synchronization is implicit because the message must be sent before the message is received.

 

java is a shared-memory model, where communication between Java threads is always implicit and the entire communication process is completely transparent to the programmer.

 

 

java threads is controlled by the Java memory model (this is referred to as JMM), and JMM determines when a thread's write to a shared variable is visible to another thread. From an abstract point of view, JMM defines the abstract relationship between threads and main memory: Shared variables between threads are stored in main memory, and each thread has a private local memory (local memory) in which the thread is stored in a copy of the read/write shared variable. Local memory is an abstract concept of JMM and is not really there. It covers caching, write buffers, registers, and other hardware and compiler optimizations. The Java memory model is abstracted as follows:

From the point of view, if you want to communicate between thread A and thread B, you have to go through the following 2 steps:

    1. First, thread a flushes the updated shared variables in local memory A to the main memory.
    2. Then, thread B goes to main memory to read shared variables that have been updated before thread A.

These two steps are described below:

 

 

Java memory model

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.