0. Why a Java memory model is required
In order for programmers to ignore the differences in memory access between various hardware and operating systems, as well as the differences in memory models on different architectures, Java provides a Java memory model between the code and hardware memory models.
I. Classification of concurrency Models
in concurrent programming, there are two key issues to deal with: how to communicate between threads (the mechanism by which a thread exchanges information, two ways of sharing memory and message passing) and how threads are synchronized.
in the concurrency model of shared memory (such as Java), the common state of the program is shared between threads, and the threads communicate implicitly (transparently to the programmer) through a common state (main memory) in the write-read memory. 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 that a program uses to control the relative order of operations between different threads. In the shared-memory concurrency model, synchronization is performed explicitly. Programmers must explicitly specify a method or a piece of code that requires mutually exclusive execution between threads. In the concurrency model of message passing, synchronization is implicit because the message must be sent before the message is received.
While Java is a shared-memory concurrency model that is transparent to programmers, but does not understand the Java memory model, there are no workarounds for memory visibility, ordering, and so on.
two. Java Virtual runtime data area
The virtual machine stack describes the memory model that Java executes: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits, and so on.
The main goal of the Java memory model is to define access rules for variables in the program, read and write the variables in the virtual machine, monitor locking and release operations, and thread start and merge operations.
three. Atomicity, visibility and ordering
The Java memory model is built around how to handle atomicity, visibility, and ordering in the concurrency process.
Atomicity: The operation can no longer be divided. Use synchronized and Reentrantlock in Java code as a guarantee. Visibility: A thread operation, visible to the B thread, both a variable assigned value 1, B thread can see the variable has become 1. Synchronized, final, volatile and reentrantlock can be provided. Order: In this thread, all operations are ordered (line range is expressed as serial semantics), and if another thread is observed in another thread, all operations are unordered (instruction reordering vs. working memory and main memory synchronization delay). Synchronized, volatile and reentrantlock provide an orderly guarantee. From the above can be concluded that synchronized and Reentrantlock is omnipotent, but the efficiency has a huge difference, reentrantlock will be much better than synchronized, concurrenthashmap the source of the implementation of the use of Re Entrantlock segmented locks to improve concurrency security and efficiency.
four. Happens-before rules
To ensure that a thread sees the results of the operation of the B-thread (whether a and B are executed in the same thread), the happens-before relationship must be met between A and B. The Happens-before rules are as follows:
Program Order rule: Action A in the program before action B, the a operation in the thread will precede the B operation.
Tube lock rule: A unlock operation Happen-before back (in chronological order, same as below) lock operation on the same lock.
Volatile variable rule: a write operation to a volatile variable happen-before the subsequent read operation on the variable.
Thread Start rule: the Start () method of the thread object Happen-before every action of this thread.
Thread termination rule: All operations of the thread happen-before the termination detection of this thread, which can be detected by means of the end of the Thread.Join () method, the return value of thread.isalive (), and so on, to detect that the thread has terminated execution.
Thread Break rule: The call to the thread interrupt () method Happen-before occurs when the code of the interrupted thread detects an outage.
Finalizer rules (Finalizer rule): The initialization of an object (the completion of a constructor execution) Happen-before the beginning of its finalize () method.
Transitivity: If operation a happen-before operation B, Operation b happen-before Operation C, then a happen-before operation C can be obtained.
If there is a lack of happens-before relationships between the two operations, the JVM can sort them arbitrarily.
five. References Java concurrency Programming-in-depth understanding of Java virtual Machines Http://www.infoq.com/cn/articles/java-memory-model -1
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JDK source reading--java memory model