Interview articles - starting today to completely distinguish between the Java memory model JMM and the runtime data area

"I believe many people will confuse the Java memory model with the Java runtime data area"

The Java memory model and the Java runtime data area are two different concepts, which can easily be confused. Here is a brief introduction to their differences:

The Java Memory Model (JMM) is a memory model defined in the Java Virtual Machine Specification, which describes how Java programs access shared memory in a multi-threaded environment. JMM is mainly concerned with issues such as visibility, atomicity, and ordering of shared variables in a multi-threaded environment.

The Java runtime data area is a memory area created by the Java virtual machine at runtime to store the data structures and object instances needed by the Java program at runtime. The Java runtime data area includes heap, method area, virtual machine stack, local method stack, and program counter.

You can tell the difference by looking at the 2 pictures below.

1. The concept of Java memory model

JMM (Java Memory Model) is a memory model defined in the Java Virtual Machine Specification, which describes how Java programs access shared memory in a multi-threaded environment. JMM is mainly a memory model defined to shield the differences in memory access by various hardware and operating systems. JMM defines an abstract computer memory model, including main memory and working memory.

The Java Memory Model (JMM) defines how Java programs access shared memory in a multithreaded environment. The Java memory model is mainly composed of two parts: main memory and working memory.
1. Main memory
The main memory is the memory area shared by all threads, and it is also the core part of the Java memory model. Instance data, class information, methods, etc. of Java objects are stored in the main memory.
In a multi-threaded environment, when a thread modifies a shared variable in main memory, other threads will not immediately see the modification of the variable. This is because each thread has its own working memory, and there is a problem of cache data inconsistency with the main memory.
2. Working memory
Each thread has its own working memory (Thread Local Memory), which is a thread-private memory area. A copy of the shared variables used by the thread is stored in the working memory.
When a thread needs to use a shared variable, it will first read the value of the variable from the main memory into its own working memory, and operate on it. After the operation is complete, the thread writes the value of the variable back to main memory. During this process, other threads cannot directly access the working memory of this thread.
3. Memory interaction operations
The Java memory model also defines some memory interaction operations, including lock, unlock, read, and write. These operations can ensure the visibility and consistency of shared variables in a multi-threaded environment.
lock and unlock: Used to lock and unlock shared variables to ensure that only one thread can access the variable at the same time.
read: used to transfer the value in the working memory to the main memory.
write: used to transfer the value in the main memory to the working memory.
4. Memory barrier
The Java memory model also defines a memory barrier (Memory Barrier), which is used to control the order and visibility of memory interaction operations.
There are four types of memory barriers:

  • LoadLoad barrier: Ensure that all load instructions before the load instruction have been executed.
  • StoreStore barrier: Ensure that all store instructions before the store instruction have been executed.
  • LoadStore barrier: Ensure that all instructions before the load instruction have been executed and the latest variable values ​​can be read.
  • StoreLoad barrier: Ensure that all instructions before the store instruction have been executed, and the variable value written by the instruction is visible to other threads.

2. Related issues

  1. What are JMMs?

JMM (Java Memory Model) is a memory model defined in the Java Virtual Machine Specification, which describes how Java programs access shared memory in a multi-threaded environment.

2. What is the function of JMM?

The main role of JMM is to ensure data visibility, atomicity and order in a multi-threaded environment. Through the specification of JMM, we can ensure the correctness and reliability of multi-threaded programs.

3. What is main memory and working memory?

The main memory is the memory area shared by all threads and is also the core part of the Java memory model. The working memory is a private memory area for each thread, which holds a copy of the shared variables used by the thread.

4. What is a memory barrier?

Memory barrier (Memory Barrier) is a mechanism used in the Java memory model to control the order and visibility of memory interaction operations. There are four types of memory barriers: LoadLoad barrier, StoreStore barrier, LoadStore barrier, and StoreLoad barrier.

5. What are the functions of keywords and APIs such as synchronized, volatile, and Lock?

These keywords and APIs are mechanisms used to achieve synchronization in Java multi-threaded programming, and are used to ensure the visibility, atomicity, and order of shared variables in a multi-threaded environment. The synchronized keyword is used to implement a pessimistic locking mechanism, the volatile keyword is used to implement a lightweight synchronization mechanism, and the Lock interface is used to implement a more flexible locking mechanism.

6. What is thread safety?

Thread safety means that in a multi-threaded environment, the program can correctly process shared data without uncertain or erroneous results. Thread safety usually requires the use of synchronization mechanisms to ensure the visibility, atomicity, and ordering of shared variables, thereby avoiding problems such as race conditions and deadlocks.

7. What is the role of the final keyword in Java?

The final keyword can be used to modify variables, methods and classes. When the final keyword modifies a shared variable, it ensures that the value of the variable will not be modified after initialization. This also means that when other threads read the value of the variable, they can obtain the latest value of the variable, thereby ensuring thread safety.

8. What is the relationship between the java memory model and the java runtime data area?

The main memory corresponds to the java heap and method area; the working memory corresponds to the java stack, and the thread is private.

Author: Xuanming Hanko
Link: https://juejin.cn/post/7220712723599081509/
Source: Rare Earth Nuggets
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

Guess you like

Origin blog.csdn.net/citywu123/article/details/130089626