"Please tell me your understanding of Happens-Before"
When you hear this question, do you know how to answer it?
Hi, my name is Mic, a Java programmer who has been working for 14 years.
Concurrent programming is the focus of the interview process, and there are many directions that can be investigated.
Regarding this question, I have compiled the expert answers into a 15W word interview document. You can privately message me to receive it.
Check out the experts' answers below.
Expert:
First, Happens-Before is a visibility model, that is, in a multithreaded environment.
Originally, the existence of instruction reordering will cause data visibility problems, that is, thread A modifies a shared variable
Not visible to B thread.
Therefore, JMM provides developers with memory visibility guarantees across threads through the Happens-Before relationship.
If the execution result of one operation is visible to another operation, then there must be Happens-Before management between these two operations.
Secondly, the Happens-Before relationship only describes the visibility of the result, and does not indicate the order in which the instructions are executed. That is to say, as long as it does not affect the result, the reordering of the instructions is still allowed.
Finally, there are many Happens-Before rules in JMM.
- The program sequence rules, each operation in a thread, and any subsequent operation in the happens-before thread can be simply considered as-if-serial, that is, no matter how reordered, the execution result of a single-threaded program cannot be changed.
Transitive rules, that is, A Happens-Before B, B Happens-Before C.
A Happens-Before C can be derived.
- The volatile variable rules, writing to a volatile-modified variable must happen-before any subsequent read operations on the volatile variable
Monitor lock rules, a thread's release lock operation for a lock must happen-before and subsequent threads lock the lock operation. In this scenario, if thread A acquires the lock and modifies x to 12, then the follow-up The value of x obtained by the thread after acquiring the lock must be 12.
Thread start rules, if thread A performs the operation ThreadB.start(), then the operation before thread A's ThreadB.start() happens-before any operation in thread B.
In such a scenario, for the assignment of x=10 before the t1 thread starts, the value of x read after the t1 thread starts must be 10.
The join rule, if thread A executes the operation ThreadB.join() and returns successfully, then any operation in thread B happens-before thread A successfully returns from the ThreadB.join() operation.
Summarize
The Happens-Before model is a rule that must be understood and mastered in multi-threaded development.
It can guide developers to avoid memory visibility problems when using multi-threaded development
Therefore, this interview question is actually a test of the basic ability of job seekers.
Remember to like and subscribe! ! !
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。