The principle of happens-before in Java
This article introduces the relevant knowledge of "happens-before principle in Java". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
There is a very important principle in Java, this principle is to determine whether there is data contention thread, thread safety is the main basis, this principle is happens-before principle. Let's first look at what exactly happens-before principle is. Simply put, the happens-before principle is the bias relationship between two operations defined in JMM. Suppose we have two operations A and B. If operation A occurs before operation B, that is, operation A occurs before operation B, then the influence of operation B after operation A occurs can be obtained. The influence here mainly includes modifying shared variables, invoking methods, etc. That's the definition of the happens-before principle. Let's use a simple example to illustrate what the happens-before principle does.
Let us assume that the three statements in the above code are executed by three threads, and suppose that the three threads are thread-1, thread-2, and thread-3. If we specify that thread-1 is to be executed before thread-2, then we conclude that thread-2 must have a value of 1 after execution. The reason is that according to the happens-before principle, thread-1 occurs first before thread-2, so the value of thread-1 after execution, thread-2 is available, and we assume that thread-3 is not executed. Then after thread-1 executes, the value of variable i has not been modified by other threads, so thread-2 gets the value of variable i as 1. Let us assume that thread-1 and thread-2 also have a happens-before relationship, i.e. thread-1 executes before thread-2 and we assume that thread-2 and thread-3 do not have a happens-before relationship. This means that thread-2 and thread-3 are not determinable. If thread-3 is executed before thread-2, what is the value of variable j in thread-2? The answer is obvious: the value of variable j is uncertain. Because we're not sure which thread-1 or thread-3 executes first. If thread-3 is executed before thread-1, then thread-1 and thread-2 have a happens-before relationship, so it has no effect on the value of variable j, but if thread-1 is executed before thread-3, then thread-2 may obtain expired data when obtaining the value of variable i, which causes a so-called thread safety problem.
Happens-before principles are present directly in JMM, we don't need any synchronization and can use them directly in coding. If the operations in the code don't have the happens-before principle, then it's possible for the virtual machine to randomly reorder them as they execute. Let's take a look at which operations in JMM have the happens-before principle by default.
Program order rule: Within a thread, code written earlier occurs before code written later in the program code order. (For the time being, branch judgment, circulation, etc. are not considered)
Rule of pipe locking: An unlock operation occurs before a subsequent lock operation on the same lock.
Volatile variable rule: A write to a volatile variable occurs before a read to the variable.
Thread start rule: The start() method of the Thread object occurs first at every action of this thread.
Thread termination rule: All operations in a thread occur prior to termination detection for this thread.
Object finalization rule: An object's initialization occurs first in its finalize() method.
Transitivity: If operation A occurs first before operation B, and operation B occurs first before operation C, then operation A also occurs first before operation C.
"Happens-before principle in Java" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!