Home  >  Article  >  Java  >  Detailed introduction about happens-before

Detailed introduction about happens-before

巴扎黑
巴扎黑Original
2017-06-14 09:46:341688browse

When executing a program, in order to provide performance, processors and compilers often reorder instructions, but they cannot be reordered at will. It is not how you want to sort it. It needs to meet the following two conditions: 1. The results of program execution cannot be changed in a single-threaded environment; 2. Reordering is not allowed if there are data dependencies. If you have read LZ’s previous blog, you will know that in fact, these two points can be attributed to one point: the happens-before principle cannot be passed By extension, JMM allows arbitrary ordering. as-if-serial semantics as-if-serial semantics means that all operations can be reordered for optimization, but you must ensure that the execution results after reordering cannot be changed, the compiler, runtime, processor All must comply with as-if-serial semantics. Note that as-if-serial only guarantees a single-threaded environment and is invalid in a multi-threaded environment. Let’s use a simple example to illustrate: int a = 1;

1. [Fighting Java Concurrency]-----Reordering of Java Memory Model

Detailed introduction about happens-before

Introduction: When executing a program, in order to improve performance, processors and compilers often reorder instructions. But you can't reorder at will. You can't sort it however you want. It needs to meet the following two conditions: 1. The result of the program running cannot be changed in a single-threaded environment; 2. Reordering is not allowed if there are data dependencies. If you look at Those who have read LZ’s previous blog will know that these two points can actually be attributed to one thing: it cannot be deduced through the happens-before principle, and JMM allows arbitrary ordering. as-if-serial semantics as-if-se

2. 【Java concurrency】-----Java memory model happens-before

Detailed introduction about happens-before

Introduction: In the previous blog ([Fighting Java Concurrency] - In-depth analysis of the implementation principle of volatile) LZ mentioned It has been seen that due to the existence of thread local memory and main memory, coupled with reordering, there will be visibility problems in multi-threaded environments. So if we use synchronization and locking correctly, when does thread A modify variable a visible to thread B? We cannot stipulate in all scenarios when variables modified by a thread are visible to other threads, but we can specify certain rules. This rule is happens-before. Starting from JDK 5, JMM uses ha

3. Non-blocking synchronization algorithm practice (3)-LatestResultsProvider

Detailed introduction about happens-before

# #Introduction: Thank you trytocatch for submitting this article. Preface Before reading this article, readers need to be familiar with happens-before and understand some basic concepts of non-blocking synchronization. This article mainly focuses on the flexible application of the happens-before rule, some tips for solving problems, and ways to analyze problems. Background introduction The original requirement is: I was writing a regular replacement tool

[Related Q&A recommendations]:

happens of java memory model- beforeSemantic order issue

The above is the detailed content of Detailed introduction about happens-before. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn