JAVA memory model that Xiaobai can understand

Time:2021-6-10

preface

Java Concurrent Programming Series has opened a hole. It can be said that Java Concurrent Programming is a necessary literacy for middle and senior R & D engineers, and it is also a necessary question for middle and senior job interviews. This series is to lead readers to break through the difficulties of Java Concurrent Programming step by step, break down the barriers, and get the desired offer, Java Concurrent Programming Series articles still use the style of pictures and text, so that Xiaobai can also understand.

Java Memory Model(Java Memory Model)AbbreviationJ M MAs the beginning of Java Concurrent Programming Series, it is the basic knowledge of Java Concurrent Programming,Understanding it will give you a better understanding of what thread safety is all about

content syllabus

JAVA memory model that Xiaobai can understand

Hardware memory model

A program is a collection of instructions and data. When a computer executes a program, it is a collection of instructions and dataC P UEach instruction is executed becauseC P UIn order to read instructions from memory, it is necessary to read and write data in memory according to instructions, so it is inevitable to deal with memory when executing instructionsC P UThere’s not much difference in processing speed, but there’s no problem.

C P U cache

along withC P UWith the rapid development of technology,C P UIt’s getting faster and faster,Memory has not changed much, resulting in memory read and write(IO)Speed andC P UIn order to solve this problem, cache is introduced(Cache)Design inC P UAnd memoryCache layerThe cache layer here refers toC P UInternalRegister and cacheL1,L2,L3

JAVA memory model that Xiaobai can understand

As can be seen from the figure above, the register is the fastest and the main memory is the slowest. The faster the memory is, the smaller the memory space isC P UThe nearer, on the contrary, the larger the storage space, the slower the speedC P UThe farther away.

How CPU interacts with memory

C P UAt run time, the instruction and data are copied from main memory to cache layer. Subsequent read-write and operation are based on the instruction and data of cache layer. After the operation, the result is written back to main memory from cache layer.

JAVA memory model that Xiaobai can understand

As you can see from the picture above,C P UBasically it’s all about talking to peopleCache layerDeal with, adoptCache designMake up for main memory andC P UThis kind of design is not only reflected in the hardware level, but also in the daily development of those business scenarios with high concurrency. However, everything has its advantages and disadvantages. Although cache speeds up the speed, it also brings problems in the multi-threaded scenarioCache consistency issues, aboutCache consistency issuesI’ll say later, let’s leave an impression here.

Java Memory Model

Java Memory Model(Java Memory Model,J M M), followed byJ M Mabbreviation,J M MIt’s built onAbstract model based on hardware memory modelIt’s not a physical memory partitionsendJavaVirtual machine(Java Virtual Machine,J V M)Achieve consistent memory interaction effect in each platform,It is necessary to shield the interaction differences between different hardware models in the downstream, unify the specification, and provide a unified interface for the upstream.

J M MIt’s a guaranteeJ V MIn each platform, the interaction of computer memory can ensure the consistent effect of the mechanism and specification

JAVA memory model that Xiaobai can understand

Abstract structure

J M MThe abstract structure is divided into thread local cache and main memory. Each thread has its own local cache, and the local cache is the threadprivateThe main memory is computer memory, which isshareYes.

JAVA memory model that Xiaobai can understand

It’s not hard to find outJ M MIt is not different from the hardware memory modelthread Analogy intoCoreThread local cacheAnalogy intoCache layer, as shown in the figure below

JAVA memory model that Xiaobai can understand

Although the memory interaction specification is good, there must be thread safety problems in multithreading scenarios(Competition and sharing of resources)In order to make multithreading execute synchronously correctly, we need to guarantee the three characteristics of concurrencyVisibility, atomicity and orderliness

visibility

When a thread changes the value of a shared variable, other threads can immediately know the change. This is calledvisibility If there is no guarantee, it will appearCache consistencyJ M MIt is stipulated that all variables are put in the main memory. When a thread uses variables, it first gets them from the cache, and then copies them from the main memory to the cache. Finally, the variables in the thread’s own cache will cause the thread to operate.

JAVA memory model that Xiaobai can understand

Thread a executes the process

  • <span style=”color: Blue;”> thread AGet variables from cachea
  • <span style=”color: Blue;”> Cache miss, copy from main memory to cache, at this timeayes0
  • <span style=”color: Blue;”> thread AGet variablea, perform the calculation
  • <span style=”color: Blue;”> Calculation results1, write cache
  • <span style=”color: Blue;”> Calculation results1, write to main memory

Thread B execution process

  • <span style=”color: Blue;”> thread BGet variables from cachea
  • <span style=”color: Blue;”> Cache miss, copy from main memory to cache, at this timeayes1
  • <span style=”color: Blue;”> thread BGet the variable a and perform the calculation
  • <span style=”color: Blue;”> Calculation results2, write cache
  • <span style=”color: Blue;”> Calculation results2, write to main memory

ABAfter the two threads are executed, the threadAAnd threadBCache data is inconsistent, which is whyCache consistency issuesOne is1The other is2, if the threadADo it again+1Operation, write to main memory or2That is to say, two threads pairaA total of 13second+1The expected result is3The final result is2

solveCache consistency issuesWe need to make sure thatvisibility , the idea is also very simple. After the variable is written to main memory, clear the variable cached by other threads, so that the cache of other threads will miss and go to main memory to load.

JAVA memory model that Xiaobai can understand

Thread a executes the process

  • <span style=”color: Blue;”> thread AGet variables from cachea
  • <span style=”color: Blue;”> Cache miss, copy from main memory to cache, at this timeayes0
  • <span style=”color: Blue;”> thread AGet variablea, perform the calculation
  • <span style=”color: Blue;”> Calculation results1, write cache
  • <span style=”color: Blue;”> Calculation results1, write to main memory, and clear the threadBcacheavariable

Thread B execution process

  • <span style=”color: Blue;”> thread BGet variables from cachea
  • <span style=”color: Blue;”> Cache miss, copy from main memory to cache, at this timeayes1
  • <span style=”color: Blue;”> thread BGet the variable a and perform the calculation
  • <span style=”color: Blue;”> Calculation results2, write cache
  • <span style=”color: Blue;”> Calculation results2, write to main memory, and clear the threadAcacheavariable

ABAfter the two threads are executed, the threadAThe cache is empty, and thread a does it again+1Operation, will be loaded from main memory (first get from cache, cache miss, and then copy from main memory to cache)2The last one written to main memory is3JavaProvided involatileModifying variable assurancevisibility (this paper focuses onJ M M, so it won’t be rightvolatileToo much reading).

It seems that all the problems have been solved, but the scenario described above is based on the ideal situation(Orderly execution of threads)In practice, threads may be concurrent(Alternate execution), or it can be parallel, only guaranteedvisibility There will still be problems, so it needs to be guaranteedAtomicity

Atomicity

AtomicityRefers to one or more operations in theC P UIn the process of execution, the feature of not being interrupted, either execution, or execution, can not be executed to half, in order to intuitively understand what isAtomicityTake a look at the code below

int a=0;
a++;
  • Atomic operation:int a=0There is only one operation, which is assignment
  • Non atomic operation:a++There are three steps: read value, calculate value and assign value

If the multithreading scenario is implementeda++Operation, only guaranteevisibility There is no guaranteeAtomicityThere will also be problems.

JAVA memory model that Xiaobai can understand

Concurrent scenario (alternate thread execution)

  • <span style=”color: Blue;”> thread ARead variableaTo the cache,ayes0
  • <span style=”color: Blue;”> conduct+1The result is obtained by operation1
  • <span style=”color: Blue;”> Switch toBthread
  • <span style=”color: Blue;”>BThe thread executes the whole process,a=1Write to main memory
  • <span style=”color: Blue;”> thread AResume the execution and get the resulta=1Write cache and main memory
  • <span style=”color: Blue;”> The final result is wrong

Parallel field (simultaneous execution of threads)

  • <span style=”color: Blue;”> thread AAnd threadBAt the same time, there may be threadsAPerform an operation+1In this case, the threadBAll the execution has been completed, or two threads may finish calculating and writing at the same time. No matter what, the result is wrong.

In order to solve this problem, as long as the multiple operations into one-step operation, that is to ensure thatAtomicity

JAVA memory model that Xiaobai can understand

JavaProvided insynchronizedAt the same time, it meets the requirements of order, atomicity and visibility)It can guarantee the atomicity of the results(Note the description here),synchronizedThe principle of atomicity is simple, becausesynchronizedYou can lock code fragments to prevent multiple threads from executing the same piece of code concurrently (this article focuses onJ M M, so it won’t be rightsynchronizedToo much reading).

JAVA memory model that Xiaobai can understand

Concurrent scenarios (threads)AAnd threadBAlternate execution)

  • <span style=”color: Blue;”> thread AGet lock successfully
  • <span style=”color: Blue;”> thread ARead variableaTo cache+1The result is obtained by operation1
  • <span style=”color: Blue;”> Now switch toBthread
  • <span style=”color: Blue;”> thread BFailed to acquire lock, blocking and waiting
  • <span style=”color: Blue;”> Switch back to threadA
  • <span style=”color: Blue;”> thread AExecute all processes, main memorya=1
  • <span style=”color: Blue;”> Thread a releases the lock successfully and notifies the threadBGet lock
  • <span style=”color: Blue;”> Thread B obtains the lock successfully and reads the variableaTo cache, wherea=1
  • <span style=”color: Blue;”> Thread B executes all the processes and saves the main memorya=2
  • <span style=”color: Blue;”> Thread B released lock successfully

Parallel scene

  • <span style=”color: Blue;”> thread AGet lock successfully
  • <span style=”color: Blue;”> thread BFailed to acquire lock, blocking and waiting
  • <span style=”color: Blue;”> thread ARead variableaTo cache+1The result is obtained by operation1
  • <span style=”color: Blue;”> thread AExecute all processes, main memorya=1
  • <span style=”color: Blue;”> thread ARelease lock successfully, inform threadBGet lock
  • <span style=”color: Blue;”> thread BAcquire lock successfully, read variableaTo cache, wherea=1
  • <span style=”color: Blue;”> thread BExecute all processes, main memorya=2
  • <span style=”color: Blue;”> thread BThe lock was released successfully

synchronizedLock the code segment of shared resources to achieve mutual exclusion effect, which naturally solves the problem that cannot be guaranteedAtomicity, visibility and orderIt’s a problem.

Although in parallel fieldAThe thread was interrupted and switched toBThread, but it still needs to waitAAfter the thread is executed, it can continue, so the atomicity of the result is guaranteed.

Orderliness

In daily writing code, we may all think that the program is executed in the order of writing, but it is not. In order to optimize performance, the compiler and processor rearrange the code, so the actual execution order of the statements may be consistent with the order of the input code, which is calledInstruction reordering

Readers may ask, “why can instruction rearrangement optimize performance?”, actuallyC P UIt will execute the rearranged instructions in parallel to optimize the performance.

Instructions before reordering

JAVA memory model that Xiaobai can understand

Reordered instructions

JAVA memory model that Xiaobai can understand

After reordering, theaThe operation instruction has changed, saving one timeLoad aandStore aThis is the benefit of reordering.

Rearrangement followsas-if-serialIn principle, compilers and processors do notThere are data dependenciesDo reordering for the operation of, because this reordering will change the execution result(That is, no matter how reordering, the execution result of single threaded program cannot be changed)The following situation is data dependence.

int i = 10
int j = 10
//This is data dependency. Int i and int j cannot be placed under int C
int c = i + j

But it’s only for single thread, and there’s no such guarantee for multithreading scenarios. SupposeA、BTwo threads, threadAThere is no data dependency in the code segment, and the thread cannot be usedBDependent threadAThe results are as follows(Assumptions guarantee visibility

JAVA memory model that Xiaobai can understand

Disable scene rearrangement (I default 0)

  • <span style=”color: Blue;”> thread Aimplementi = 10
  • <span style=”color: Blue;”> thread Aimplementb = true
  • <span style=”color: Blue;”> thread Bimplementif( b )Pass the verification
  • <span style=”color: Blue;”> thread Bimplementi = i + 10
  • <span style=”color: Blue;”> final resultiyes20

Rearrange scene (I default 0)

  • <span style=”color: Blue;”> thread Aimplementb = true
  • <span style=”color: Blue;”> thread Bimplementif( b )Pass the verification
  • <span style=”color: Blue;”> thread Bimplementi = i + 10
  • <span style=”color: Blue;”> thread Aimplementi = 10
  • <span style=”color: Blue;”> final resultiyes10

To solve the problem of reordering, we use thevolatileModifying variables while ensuringVisibility and order, byvolatileThe modified variable will be added withSort forbidden by memory barrier(this paper focuses onJ M M, so it won’t be rightvolatileToo much reading).

Guarantee of three characteristics

characteristic volatile synchronized Lock Atomic
visibility upon sb.’s honor upon sb.’s honor upon sb.’s honor upon sb.’s honor
Atomicity There is no guarantee upon sb.’s honor upon sb.’s honor upon sb.’s honor
Orderliness A certain degree of guarantee upon sb.’s honor upon sb.’s honor There is no guarantee

About me

This is a Xing, a Java program that loves technology, ape, official account.Program ape starIn 2021, we will share with you the excellent original articles of operating system, computer network, Java, distributed, database and so on!.

Thank you very much for reading here. It’s not easy to be original. Articles can be helpful. You can “like” or “share and comment”. They are all supportive (don’t go whoring in vain)!

I hope you and I can go on the way we want to go. I’ll see you in the next article!

Make a friend
JAVA memory model that Xiaobai can understand

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]