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 the use of the volatile keyword in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what is the use of the volatile keyword in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what's the use of the volatile keyword in Java"?

I. the function of volatile

It can ensure the visibility of shared variables in multithreaded environment.

Prevent reordering between multiple instructions by adding a memory barrier

2. What is visibility

Visibility means that when one thread modifies a shared variable, other threads can immediately see a value after the modification. Visibility is essentially caused by several aspects.

Cache at the cpu level, in cpu, uh, a three-level cache is designed to solve the problems of cpu operation efficiency and memory IO efficiency, but it also brings a problem of cache consistency, and in the case of multithreaded execution, the problem of cache consistency will lead to the problem of visibility. Therefore, for a modified shared variable that adds the volatile keyword, the jvm virtual machine automatically adds a # lock assembly instruction. And this instruction will automatically add bus locks or cache locks according to different cpu models.

What are bus locks and cache locks

The bus lock locks the cpu front-end bus so that only one thread can communicate with memory at a time, thus avoiding the visibility problem caused by multithreading concurrency.

The cache lock is an optimization of the bus lock, because the bus lock leads to a significant decrease in the efficiency of cpu, so the cache lock is only targeted at the target data in the cpu three-level cache, while the cache lock is implemented using the MESI cache consistency protocol.

IV. What is the order reordering

Instruction reordering is inconsistent in the order of data and execution, which leads to visibility problems in multithreaded environment. Instruction reordering is essentially a means of performance optimization, and instruction reordering comes from several aspects.

At the cpu level, aimed at further optimizing the MESI protocol to improve the utilization of cpu, he introduced a mechanism called StoreBuffer, and this optimization mechanism will lead to out-of-order execution of cpu. In order to avoid this problem, cpu provides memory barrier instructions, and upper applications can insert memory barriers in appropriate places to avoid a problem of reordering cpu instructions.

In the process of compilation, the compiler reorders instructions reasonably without changing the semantics of a single thread and the correctness of the program, so as to optimize the overall performance. So shared variables add the volatile keyword, so the compiler level will not trigger compiler optimization. At the same time, in jvm, it will insert memory barrier instructions to avoid reordering.

In addition to the volatile keyword, developed from JDK5, JMM uses a Happens-Before model to describe a relationship of multithreaded visibility, that is, there is a Happens-Before relationship between two operations, so the two operations have a visibility relationship, so there is no need to consider adding the volatile keyword to provide a guarantee of visibility.

At this point, I believe you have a deeper understanding of "what is the use of the volatile keyword in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow 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