He has recently found himself on the lack of product thinking, and just finished reading "java concurrent programming art of the book", and decided to demand from the perspective of programmers probably write about their own understanding of the JMM, and we want to help.
background knowledge
Learn a solution first have to understand the issues it deals with, by background knowledge, we can better understand the reasons for it, and thought contained therein
Enhance the processing speed in a single processor no longer apparent, people will try to use a variety of methods to improve the operation efficiency of the computer, including
- Introduction of multi-level caching mechanism, with the reading operation by reducing the data, calculation results stored in I / O operation times, so that the computer operation performed quickly, reducing the processor latency
- The use of multi-core processors, Three Stooges Zhuge Liang had a roof over the top
- Code optimization scrambled, in order to re-adjust the underlying code may be implemented using hardware batch technology to accelerate the operation efficiency
But at the same time to introduce a new concept, often accompanied by many problems waiting to solve these great pioneers.
Cache coherency problem
10/13/16dc57096e9b6482?w=1003&h=716&f=png&s=54796)When a plurality of processors to be used and modifications in the main memory at the same time a region may result in different data processors are not the same, how data from different CPU registers, cache synchronized to ensure data consistency, computing At the same time to ensure the correctness of the efficiency of the processor design issues to consider. In hardware, the processors have followed their own coherence protocols to address these issues
Instruction reordering problem
# CPU1 CPU1
a = 2 b = 100
b = 100 a = 2
print(a + b) print(a + b)
复制代码
Reordering can effectively improve the operational efficiency of the processor, but the result of reordering the results sometimes make errors, especially in a multicore environment, a lot of wonderful problem occurs, the following two CPU Guess output
# CPU1 CPU2
a = 10 b = 20
b = a + 100 a = b + 100
print(f"b = {b}") print(f"a = {a}")
复制代码
If the above procedure will find multiple runs, in the terminal actually had a = 100, b = 100 is the result, because the underlying processor "opinionated" reordered so we run the program becomes as follows
# CPU1 CPU2
a = 10 b = 20
print(f"b = {b}") print(f"a = {a}")
b = a + 100 a = b + 100
复制代码
Distinguish between the various hardware
To solve the problem caused by multi-core processors, with different designs offer different programs, but for programmers to learn these strategies and write correct programs have great difficulty
Programmer's expectations for JMM
In life we often do not party, but for JAVA designers are concerned, we are the Party, and much of our requirements, but also not too much
Sequential consistency
When we programmers to write correct code premise, we want the program in accordance with the order of the code envisaged execution, and output the correct result
So in the design, memory model and programming language processor memory consistency model will be in the order of reference, we took specific order to talk about the consistency of the specific requirements
Data reading and writing sequential consistency
When we deal with multi-threading problems often have problems following data competition:
- Thread 1: read the x variable memory area
- Thread 2: Update the x variable memory prefetching
- How to synchronize two or more operations?
As a programmer we expect, when we agreed to the operation of the thread 1 thread 2 before the execution (and vice versa), then this program will become a program no data race, but we can also call the program correctly synchronized , execution of the program will have a sequence identity
Threads and sequential consistency
When we run multiple threads of time, the sequential consistency assures us:
- All operations must be performed in a thread in program order (without reordering instructions), all operations can only see a single sequence of operations performed (parallel to serial conversion)
- Each operation must be atomic execution and immediately visible to all threads. (Note: this refers to the atomic execution operation, once started, it runs to completion, without any intermediate thread switching)
! [Atomic execution] ( user-gold-cdn.xitu.io/2019/
Sync blocks and sequential consistency
class SynchronizedExample {
int a = 0;
boolean flag = false;
public synchronized void writer() {
a = 1;
flag = true;
}
public synchronized void reader() {
if (floag) {
int i = a;
...
}
}
}
复制代码
, Writer () and Reader () method, are executed in the thread 1 in the above example two threads and the code, we also desirable to perform sync blocks also satisfy the thread sequential consistency .
Operational efficiency of the code
Meet sequential consistency conditions, we hope the sooner the better code efficiency
Code readability and ease of implementation
We hope JAVA designers were able to provide safe and easy to implement mechanisms to meet the order of consistency and efficiency, and let us write code with good readability.
JMM's birth
Efficiency and consistency of the Game
We can see from the above, the order of consistency and efficiency are mutually contradictory, so JAVA designers made the following decision
Under the premise of meeting the results of the programmers desired result, as much as possible to reduce the order has been bound to the memory of the model
This paper presents a happens-before to describe this design principle, we will not do in-depth explanation for this concept, but we can understand, the designers created a program for programmers is consistency in the order of execution Utopia, it does not violate the programmer the desired results, but the actual process is different.
Language Design
java designers to achieve readability and implement, providing a bit of keywords and methods
- synchronized keyword
- volatile keyword
- final keyword
- lock
- concurrent包
JMM is these small components
to sum up
This paper describes the background knowledge of the birth of the JMM, mainly describes the contents of the hardware, and from a programmer's point of view, JMM what conditions, and the general idea of its design should meet
reference
- "Java concurrent programming art"
- Understand java memory model
- Redis depth Adventures: Core Principles and Applications Practice