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

What is java volatile?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "what java volatile is", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "Java volatile is what"!

Basic concepts of memory model

When a computer executes a program, each instruction is executed in the CPU, and in the process of executing instructions, it is bound to involve reading and writing data. Because the temporary data in the process of program operation is stored in main memory (physical memory), there is a problem at this time. Because the CPU executes very fast, and the process of reading data from memory and writing data to memory is much slower than the CPU executing instructions. Therefore, if the operation on data at any time has to be carried out through the interaction with memory, it will greatly reduce the speed of instruction execution. So there's a cache inside the CPU.

Three concepts in concurrent programming

1, atomicity

That is, an operation or operations, either all executed and the process executed is not interrupted by any factor, or none of them is executed.

2. Visibility

When multiple threads access the same variable, one thread modifies the value of the variable, and the other threads immediately see the modified value.

3. Order

That is, the order in which programs are executed follows the order in which the code is executed. Generally speaking, in order to improve the efficiency of program operation, the processor may optimize the input code. It does not guarantee that the execution order of each statement in the program is consistent with the order in the code, but it will ensure that the final execution result of the program is consistent with the execution result of the code order.

Java memory model

Java Memory Model (JMM) is defined in the Java Virtual Machine specification to shield memory access differences between hardware platforms and operating systems, so that Java programs can achieve consistent memory access effects on various platforms. So what does the Java memory model specify? It defines rules for accessing variables in a program and, more broadly, the order in which the program executes. Note that the Java memory model does not restrict the execution engine from using processor registers or caches to speed up instruction execution, nor does it restrict the compiler from reordering instructions for better execution performance. That is, cache coherency issues and instruction reordering issues also exist in the java memory model.

Note that the working memory in the figure actually corresponds to cpu cache at the hardware level.

volatile keyword

It guarantees visibility and order, but it does not guarantee atomicity.

The following is an excerpt from Understanding Java Virtual Machines:

"Observe the assembly code generated when adding volatile keyword and when not adding volatile keyword. It is found that when adding volatile keyword, there will be an extra lock prefix instruction"

The lock prefix instruction is actually a memory barrier (also known as a memory fence) that provides three functions:

1. It ensures that instruction reordering does not place subsequent instructions before the memory barrier, nor does it place preceding instructions behind the memory barrier; i.e., by the time the instruction reaches the memory barrier, all operations preceding it have been completed;

2. It forces changes to the cache to be written immediately to main memory;

3. If it is a write operation, it causes the corresponding cache line in the other CPU to be invalidated.

At this point, I believe that everyone has a deeper understanding of "what java volatile is", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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: 0

*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