Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master Java concurrent memory model

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to master the Java concurrent memory model". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

1. Java memory model

2. Hardware memory architecture

3. Actual implementation

3.1 shared object visibility

3.2 competitive conditions

Java is a language that supports multithreaded execution. To write correct concurrent programs, it is important to understand the Java memory model. Understanding the hardware memory model helps to understand the execution of the program.

The main contents of this article are as follows

Java memory model

Hardware memory architecture

Shared object visibility

Competitive conditions

1. Java memory model

The latest revision of the Java memory model is in Java5. JSR-176 lists J2SE5.0-related release features, including JSR-133 (JavaTM memory model and threading specification), which the java virtual machine follows. This memory model still works in Java8 today.

The full name of JSR is Java Specification Requests, which means the formal request for standardized technical specifications of Java.

The Java program runs on a virtual machine (Jvm). Logically, Jvm memory is divided into thread stacks and heaps. Each thread has its own stack, and the data stored in that thread's stack is not visible to other threads. Heap memory is used to store shared data.

The thread stack stores all local variables in the method, including primitive types (boolean,byte,short,char,int,long, float,double) and object references.

Heap storage requires shared objects and static variables.

Note: objects are not necessarily stored in heap memory. Looking at the following example, if the Object object does not need to be shared by other threads, the compiler performs a heap allocation and converts it to a stack allocation.

To explain, the compiler makes optimizations based on whether the object escapes. One of the optimizations is to convert heap allocation into stack allocation, which is designed to reduce GC pressure and improve performance. This optimization action is controlled by the Jvm parameter-XX:+DoEscapeAnalysi. Java8 is enabled by default.

Test: determine the storage location of the object by turning on or off-XX:+PrintGC-XX:-DoEscapeAnalysis to see if GC is executed.

Public static void main (String [] args) {for (int I = 0; I)

< 10000000; i++){ createObj(); } } public static void createObj(){ new Object(); } 2、硬件内存架构 如下图,现代计算机通常都装有2个或者更多的CPU,CPU又可以是多核。一个CPU包含一组寄存器,每个CPU具有一个高速缓存,而高速缓存又分为L1,L2,L3,L4 不同层级缓存。 RAM为主存储也就是我们说的计算机内存,所有CPU都可以读取主存储。 当CPU读取主存储数据时,它会将部分主存储数据读入CPU高速缓存中,又将缓存的中一部分读入寄存器执行,操作结束后,将值从寄存器刷新到高速缓存中,高速缓存在特定的时刻将数据统一刷新到内存中。

3. Actual implementation

In fact, the Java stack memory model described above is abstracted to understand. The actual execution is like the following figure, where thread stack and heap data may be distributed to different storage areas of the hardware. The dispersion of data in different regions brings the following two main problems.

3.1 shared object visibility

In the following scenario, two threads operate on the object obj.count at the same time, and one thread updates the obj.count, but is not visible to the other threads.

When thread An operates obj, it first copies a copy of the data from the main memory to the CPU cache, then to the register, then modifies the obj.count=2 and flushes it to the CPU cache, but the data is not synchronized to the main memory yet. At the same time, thread B also operates obj, and the copy of the data is still obj.count=1, which will result in an error in the program result.

To solve this problem, you can use the Java volatile keyword. Volatile can be simply understood as skipping the CPU cache and synchronizing the modification results to main memory in time, thus ensuring that other threads can read the latest values. The later stage of volatile is specially introduced.

3.2 competitive conditions

On the other hand, a race condition occurs if multiple threads change the line obj.count at the same time.

The solution is to use Java synchronized to ensure thread execution order, and all variables in the synchronized package are read directly from main memory (skipping the CPU cache), and when the thread exits synchronized, all updated variables are synchronized to main memory.

That's all for "how to master the Java concurrent memory model". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 253

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report