Deep parsing of Java Memory Model: basic part — conversion
Original address: http://www.codeceo.com/article/java-memory-1.html
Classification of concurrent programming models
In concurrent programming, we need to deal with two key problems: how to communicate between threads and how to synchronize between threads (threads here refer to active entities executed concurrently). Communication refers to the mechanism by which threads exchange information. In imperative programming, there are two communication mechanisms between threads: shared memory and message passing.
In the concurrency model of shared memory, threads share the common state of the program, and threads communicate implicitly by writing reading the common state in memory. In the concurrency model of message passing, there is no common state between threads, and threads must explicitly communicate by sending messages.
Synchronization is a mechanism used by programs to control the relative order of operations between different threads. In the shared memory concurrency model, synchronization is explicit. You must explicitly specify that a method or piece of code needs to be mutually exclusive between threads. In the concurrency model of message passing, synchronization is implicit because the message must be sent before the message is received.
Java concurrency adopts the shared memory model. The communication between Java threads is always implicit, and the whole communication process is completely transparent to programmers. If Java programmers who write multithreaded programs do not understand the working mechanism of implicit communication between threads, they are likely to encounter various strange memory visibility problems.
Abstraction of Java Memory Model
In Java, all instance fields, static fields and array elements are stored in heap memory, There is sharing between threads in the heap (this article uses the term "shared variable" to refer to instance fields, static fields and array elements), local variables, method definition parameters (called formal method parameters in the Java language specification) and exception handler parameters (exception handler parameters) will not be shared between threads. They will not have memory visibility problems and will not be affected by the memory model.
The communication between Java threads is controlled by the Java Memory Model (hereinafter referred to as JMM). JMM determines when the writing of shared variables by one thread 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), the local memory stores the copy of the thread to read / write shared variables. Local memory is an abstract concept of JMM and does not really exist. It covers cache, write buffer, register and other hardware and compiler optimizations. The abstract diagram of JAVA memory model is as follows: