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 analyze Volatile variables

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

Share

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

Today, I will talk to you about how to analyze Volatile variables, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

In programming, especially in C, C++, C #, and Java languages, variables or objects declared using the volatile keyword usually have special properties related to optimization and / or multithreading. Typically, the volatile keyword is used to prevent the compiler from performing any optimizations on code that it believes that the value of a variable cannot be changed "by the code itself". In C environments, the true definition and scope of application of the volatile keyword are often misunderstood, and C++, C #, and Java all mysteriously "inherit" volatile from C, where the usage and semantics of volatile are quite different.

Java also supports the volatile keyword, but it is used for other different purposes. When volatile is used in a scope, Java guarantees the following:

1. (for all versions of Java) reading and writing a volatile variable has a global sort. That is, when each thread accesses a volatile scope, it reads its current value before continuing execution, rather than (possibly) using a cached value directly from main memory to avoid compiler optimization. (however, it does not guarantee the relative order of reading and writing when reading and writing volatile scopes frequently, which means that this is not usually a useful thread build.)

2. (for Java5 and later versions) the read and write of volatile establishes a happens-before relationship, similar to applying for and releasing a mutex.

At this time, another key word associated with it is synchronized. It can be used to lock objects and methods or blocks of code, and when it locks a method or block of code, at most one thread executes this piece of code at a time. When two concurrent threads access this locked synchronization block in the same object object, only one thread can be executed at a time. Another thread must wait for the current thread to finish executing the code block before it can be executed. However, when one thread accesses a locked code block of an object, another thread can still access the unlocked code block in that object.

Therefore, the difference between the two keywords is as follows:

1.volatile essentially tells jvm that the value of the current variable in the register (working memory) is uncertain and needs to be read from main memory; synchronized locks the current variable so that only the current thread can access it, and other threads are blocked.

2.volatile can only be used at the variable level; synchronized can be used at the variable, method, and class levels.

3.volatile can only realize the visibility and atomicity of variables, while synchronized can guarantee the visibility and atomicity of variables.

4.volatile does not cause thread blocking; synchronized may cause thread blocking.

Variables marked by 5.volatile are not optimized by the compiler; variables marked by synchronized can be optimized by the compiler.

After reading the above, do you have any further understanding of how to analyze Volatile variables? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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

Servers

Wechat

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

12
Report