In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the rules and usage of happens-befor in Java". In daily operation, I believe that many people have doubts about the rules and usage of happens-befor in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "the rules and usage of happens-befor in Java". Next, please follow the editor to study!
I. happens-before rules
We write programs have to be optimized (the compiler and processors will optimize our programs to improve efficiency) will be run, optimization is divided into many kinds, one of which is called reordering, reordering needs to comply with happens-before rules, can not say you want to arrange as you like, if that is not a mess.
Some of the happens-before rules are as follows:
1. Program order rules: each operation in a thread happens-before any subsequent operations in that thread
2. Monitor lock (synchronization) rule: for the unlocking of a monitor, happens-before subsequently locks the monitor
Note 1: why is it part of the happens-before principle? because this article is for you to understand the happens-before principle, I will try my best to keep you focused on this matter and not be affected by others.
Note 2: each operation happens-before in the program sequence rule does not mean that the previous operation must be performed before the latter operation, but that the execution result of the previous operation must be visible to the latter operation. If this requirement is not met, then the reordering of the two operations is not allowed.
The function of the following method is to calculate the area of a rectangle
Public double rectangleArea (double length, double width) {double leng;double wid;leng=length;//Awid=width;//Bdouble area=leng*wid;//Creturn area;}
The above operation may be optimized by the compiler and processor before it is run
In the program
A happens-before B
B happens-before C
A happens-before C / / happens-before has transfer rules
According to the happens-before rule, we will analyze the possible results after reordering.
Because A happens-before B, the result of An operation leng must be visible to B operation, but now B operation does not use length, so these two operations can be reordered. Can An operation be reordered with C operation? if An operation and C operation are reordered, because leng is not assigned, leng=0,area=0*wid is area=0. This result is obviously wrong, so the An operation cannot be reordered with the C operation (that is, the execution result of the previous operation mentioned in note 2 must be visible to the Hou Yi operation. If this requirement is not met, reordering of these two operations is not allowed)
At this point, the study of "the rules and usage of happens-befor in Java" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.