In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is happens-before". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The literal translation of happens-before means that it happens first, and A happens-before B means that An occurs first in B?
It's not accurate! In the Java memory model, happens-before should be translated as: the result of the previous operation can be obtained by subsequent operations. To put it bluntly, the previous operation assigns the variable a to 1, and the latter operation must know that a has become 1.
Let's take a look at why these rules are needed.
Because our computers are all multi-CPU and all have caches, this leads to direct visibility problems with multithreading. For more information, please see the concurrent bug source of my previous article.
So in order to solve the visibility problem of multithreading, we came up with the happens-before principle, which allows threads to follow these principles. The compiler will also optimize our statements, so it is tantamount to giving the compiler optimization constraints. Can not let it optimize do not know the east, south, west and west!
Let's take a look at these rules.
Program order rules: the execution results of a piece of code within a thread are orderly. It will also rearrange the instructions, but no matter how it is arranged, the result will not change according to the order of our code!
Pipe locking rule: whether in a single-threaded or multithreaded environment, for the same lock, after one thread unlocks the lock, another thread acquires the lock and can see the result of the previous thread's operation! (pipe program is a general synchronization primitive, and synchronized is the implementation of pipe program)
Volatile variable rule: if a thread writes a volatile variable and then a thread reads it, the result of the write operation must be visible to the thread reading it.
Thread startup rule: during the execution of main thread A, child thread B is started, then the result of thread A's modification of shared variables before starting child thread B is visible to thread B.
Thread termination rule: when child thread B terminates during the execution of main thread A, the modification of shared variable by thread B before termination is visible in thread A.
Thread interrupt rule: the call to the thread interrupt () method first occurs when the interrupted thread code detects the occurrence of the interrupt event, and the interrupt can be detected by Thread.interrupted ().
Transitive rule: the simple thing is that the happens-before principle is transitive, that is, A happens-before B, B happens-before C, then A happens-before C.
Object termination rule: this is also simple, which is the completion of the initialization of an object, that is, the end of the constructor execution must happens-before its finalize () method.
These rules are for us developers, and mastering these rules will enable us to better develop code that meets our expectations of concurrent programs!
That's all for "what happens-before is". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.