JMM of JUC Concurrent Programming

Table of contents

1. Memory model JMM

1.1 Main memory and working memory

1.2 Reordering


1. Memory model JMM

The Java memory model is a set of rules defined in the Java Virtual Machine (JVM) specification, which is used to shield the memory access differences of various hardware and operating systems, and ensure the correct execution of programs under multi-threaded conditions. The Java Memory Model specifies how threads interact and the relationship between threads and memory. The main problems it solves are visibility, atomicity and order.

  • Visibility : Visibility means that when a thread modifies the value of a shared variable, other threads can immediately see the modification. If one thread's modification of a shared variable is not visible to other threads, it may lead to data inconsistency and erroneous results.

  • Atomicity : Atomicity means that an operation is uninterruptible, either all are executed successfully, or none are executed. In a multi-threaded environment, if an operation is non-atomic, it may cause data errors or race conditions.

  • Ordering : Ordering means that the order of program execution is consistent with the order of code. In a multi-threaded environment, due to the reordering of instructions and the interaction between threads, the execution order of instructions may be inconsistent with the writing order of codes, resulting in erroneous results.

1.1 Main memory and working memory

In the Java memory model, the main memory (Main Memory) is the memory area shared by all threads, and each thread has its own working memory (Working Memory). A thread's working memory holds a master copy of the variables used by that thread.

During execution, the thread will copy the variables that need to be used from the main memory to its own working memory for operation, and then refresh the modified value back to the main memory. This approach reduces access to main memory and improves performance. But it can also cause visibility problems, that is, the modification of a shared variable by one thread is not visible to other threads.

 Then the problems that may be encountered are as follows:

 So the visibility in JMM is that when a thread modifies the value of a shared variable, other threads can immediately see the modification.

1.2 Reordering

Reordering is a concept in computer programming and optimization, which refers to changing the order in which instructions are executed in a program to improve performance or meet specific optimization requirements. Reordering is a common optimization in modern computer systems, but it can also cause some potential problems.

In programming languages, due to data dependencies and memory model constraints, compilers and processors have a certain degree of freedom to adjust the execution order of instructions when executing instructions. These optimizations do not change the semantics of the program, i.e. the behavior of the program should be the same as without reordering, but may affect the visible behavior of the program.

There are three main types of reordering:

  1. Compiler Reordering (Compiler Reordering) : When the compiler generates target code, it may perform instruction rearrangement according to the characteristics of the target architecture to improve code execution efficiency. Compiler reordering is usually only done when it does not affect program semantics.

  2. Processor Reordering : Modern processors have out-of-order execution characteristics, and they can dynamically adjust the execution order of instructions according to the data dependencies and resource conflicts between instructions. Processor reordering can improve the execution efficiency and parallelism of instructions to a certain extent.

  3. Memory Reordering : There are features such as multi-level cache and out-of-order execution in modern computer systems, which may lead to reordering of memory read and write operations on different processors. Memory reordering can cause visibility issues where modifications to shared variables by one thread are not visible to other threads.

So the ordering feature in JMM is for reordering.

Guess you like

Origin blog.csdn.net/qq_43649937/article/details/132019929