In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the use of java cache line and volatile". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of java cache line and volatile"?
CPU in order to execute code faster. So when you read data from memory, you don't just read what you want. Instead, it reads enough bytes to fill the cache line. Cache rows vary in size depending on the CPU. For example, X86 is 32BYTES and ALPHA is 64BYTES. And always align at the 32nd or 64th byte. In this way, when CPU accesses adjacent data, it does not have to be read from memory every time, which increases the speed. Because it takes much more time to access memory than to access the cache.
As you can see, CPU is very witty. Each read not only reads the data needed this time into the cache, but also reads the adjacent data into the cache, but there is a problem. Now CPU is multi-core. If a data is cached by multiple cores at the same time, one of the cores modifies the value of this data, the cache values of the other cores will become invalid, and then the program will go wrong.
So how to deal with this situation? In java, you can add the volatile keyword to the field, and the field that uses the volatile keyword will force the cache of other CPU to invalidate after the value is changed, that is, after another CPU changes the volatile-modified data, the CPU will re-read the data from memory instead of from the cache, ensuring that it is reading new data rather than invalid data.
As you can see, volatile solves a problem that another CPU does not know after changing the data, but volatile can reduce the efficiency of the program if it is not used properly, and the performance problems caused by a volatile are analyzed in the next section.
Thank you for your reading, the above is the content of "what is the use of java cache line and volatile". After the study of this article, I believe you have a deeper understanding of the use of java cache line and volatile, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.