Summary:In this article, we will combine the case program to illustrate the Happens-Before principle in the Java memory model.

This article is shared from Huawei Cloud Community “[High Concurrency]Understand the Happens-Before Principle in Seconds”, author: Binghe.

Before formally introducing the Happens-Before principle, let’s look at a piece of code.

【Example 1】

class VolatileExample {
 int x = 0;
 volatile boolean v = false;
 public void writer() {
    x = 42;
    v = true;
 public void reader() {
 if (v == true) {

The above example is from:

Here, assuming that thread A executes the writer() method, v=true will be written into the memory according to volatile; thread B executes the reader() method, according to volatile, thread B will read the variable v from memory, if thread B reads The variable v is true, so what is the value of the variable x at this time? ?

The intuition of this sample program is that the value of x is 42. In fact, the value of x depends on the version of JDK. If the version of JDK used is lower than 1.5, the value of x may be 42 or 0. If you use JDK 1.5 and above, the value of x is 42.

Seeing this, someone will ask a question? Why is this? In fact, the answer is that the Happens-Before principle was introduced in the Java memory model in JDK1.5.

Next, we will combine the case program to illustrate the Happens-Before principle in the Java memory model.

[Principle 1]Rules of procedural order

In a thread, according to the order of the code, the previous operation Happens-Before any subsequent operation.

For example, the program x=42 in[Example 1]will be executed before v=true. This rule is more in line with single-threaded thinking: in the same thread, the modification of a variable by the program in the front must be visible to subsequent operations.

[Principle 2]volatile variable rules

A write operation to a volatile variable happens-before subsequent read operations on this variable.

That is to say, a write operation using a volatile variable occurs first before a read operation on the variable occurs later. This needs to be understood by everyone.

[Principle 3]Transmission rules

If A Happens-Before B, and B Happens-Before C, then A Happens-Before C.

Let’s look at the[Example 1]program in combination with[Principle 1][Principle 2]and[Principle 3]. At this point, we can draw the following conclusions:

(1) x = 42 Happens-Before write variable v = true, which conforms to[principle 1]program sequence rule.

(2) Write variable v = true Happens-Before read variable v = true, in line with[Principle 2]volatile variable rules.

According to[Principle 3]transmission rules, we can draw a conclusion: x = 42 Happens-Before read variable v=true.

That is to say, if thread B reads v=true, then the x = 42 set by thread A is visible to thread B. In other words, thread B at this time can access x=42.

In fact, the Java 1.5 version of the java.util.concurrent tool relies on volatile semantics to achieve visibility.

[Principle 4]Locking Rules

The unlocking operation of a lock happens-before the subsequent locking operation of the lock.

For example, the following code will automatically lock before entering the synchronized code block, and will automatically release the lock after the code block is executed.

【Example 2】

public class Test{
 private int x = 0;
 public void initX{
 synchronized(this){ //自动加锁
 if(this.x < 10){
 this.x = 10;
 } //自动释放锁

We can understand this program in this way: Suppose the value of variable x is 10, thread A changes the value of variable x to 10 after executing the synchronized code block, and releases the synchronized lock. When thread B enters the synchronized code block, it can obtain the write operation of thread A to variable x, that is, the value of variable x accessed by thread B is 10.

[Principle 5]Thread startup rules

If thread A calls the start() method of thread B to start thread B, then start() operates Happens-Before any operation in thread B.

We can also understand the thread startup rules in this way: after thread A starts thread B, thread B can see the operation of thread A before thread B is started.

Let’s look at the code below.

【Example 3】

Thread threadB = new Thread(()->{
x = 100;

The above code is a code fragment executed in thread A. According to[Principle 5]thread startup rules, after thread A starts thread B, thread B can see the operation of thread A before thread B is started, and access in thread B to the x variable with a value of 100.

[Principle 6]Thread Termination Rules

Thread A waits for thread B to complete (implemented by calling thread B’s join() method in thread A), and when thread B completes (thread A calls thread B’s join() method to return), thread A can access thread B’s pair of Operations on shared variables.

For example, the following operations are performed in thread A.

【Example 4】

Thread threadB = new Thread(()-{
    x = 100;

[Principle 7]Thread Interruption Rules

The call to the thread interrupt() method happens-before the code of the interrupted thread detects the occurrence of an interrupt event.

For example, the program code below. Before thread A interrupts thread B, modify the value of the shared variable x to 100, then when thread B detects an interrupt event, the value of the accessed variable x is 100.

【Example 5】

 private int x = 0;
 public void execute(){
 Thread threadB = new Thread(()->{
 if (Thread.currentThread().isInterrupted()){
        x = 100;

[Principle 8]The principle of object termination

An object’s initialization completes Happens-Before the start of its finalize() method.

For example, the program code below.

【Example 6】

public class TestThread {
 public TestThread(){
 protected void finalize() throws Throwable {
 public static void main(String[] args){
 new TestThread();

The running results are shown below.


Click to follow and learn about Huawei Cloud’s fresh technologies for the first time~

#Interpretation #HappensBefore #Principles #Java #Memory #Model

Leave a Comment

Your email address will not be published. Required fields are marked *