Home Java javaTutorial Detailed introduction about happens-before

Detailed introduction about happens-before

Jun 14, 2017 am 09:46 AM

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management? Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

See all articles