In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 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 concept of atomicity, visibility and order of java". The content of the explanation 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 concept of atomicity, visibility and order of java".
Atomicity
Atomicity means that the operation of a thread cannot be interrupted by other threads, and only one thread operates on a variable at a time. In the case of multithreading, the execution result of each thread is not disturbed by other threads, for example, multiple threads simultaneously perform 100 responses to the same shared member variable, and if the initial value of n is 0, the final value of n should be 100, so they do not interfere with each other, which is the legendary atomicity. But nasty + is not an operation of atomicity. AtomicInteger should be used to guarantee atomicity.
Visibility
Visibility refers to whether a thread modifies the value of a shared variable and whether other threads can see the modified value of the shared variable. There is certainly no such problem in a single thread, which must read the latest values, but not necessarily in multithreaded programming.
Each thread has its own working memory, the thread first reads the value of the shared variable from the main memory to the working memory to form a copy, and then brushes the value of the copy back to the main memory after calculation, from reading to finally brushing back to the main memory, which is a process. When it has not been brushed back to the main memory, it is not visible to other threads, so the value read by other threads from the main memory is the old value before modification.
There are visibility issues such as CPU cache optimization, hardware optimization, instruction rearrangement, and optimization of JVM compilers.
Order
We all know that programs are executed in code order, which is true for single-threading, but not in the case of multithreading. In order to optimize program execution and improve the processing performance of CPU, both JVM and the operating system will rearrange instructions, that is, the previous code may not always be executed in front of the later code, that is, the later code may be inserted in front of the previous code, as long as it does not affect the execution result of the current thread. Therefore, instruction rearrangement will only ensure that the execution results of the current thread are consistent, but the execution results of multi-threads will be affected after instruction rearrangement.
Although reordering optimizes performance, it also follows some rules and cannot be ordered randomly, but reordering will affect the results of multithreaded execution.
Thank you for your reading, the above is the content of "what is the concept of atomicity, visibility and order of java". After the study of this article, I believe you have a deeper understanding of what the concept of atomicity, visibility and order of java is, 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.