happened-before in JMM

内存一致性模型

内存一致性模型是指在共享内存环境中,多个处理器或线程之间的内存访问所遵循的规则。这些规则规定了对共享内存的访问顺序,保证了多个处理器或线程之间的内存访问是有序的,而不是随机的。

在多核处理器或者多线程应用中,存在多个处理器或线程同时访问共享内存的情况。如果这些处理器或线程之间的内存操作没有按照规定的顺序执行,就会导致数据一致性问题,例如数据竞争、死锁等等。

为了避免这种问题,需要建立一种内存一致性模型,规范处理器或线程之间的内存读写操作。这个模型通常由处理器架构或操作系统制定,并且在硬件上实现。

常见的内存一致性模型包括:

1.顺序一致性模型:所有的内存读写操作都是串行执行的,且所有的处理器或线程看到的内存访问顺序都是一致的。

2.弱一致性模型:内存读写操作可以乱序执行,但是一定会保证最终的结果是正确的。

3.松散一致性模型:内存读写操作可以乱序执行,并且不一定要保证最终结果是正确的,但是会保证最终结果是相对一致的,也就是说每个处理器或线程看到的内存访问顺序是不同的,但是最终结果是相对一致的。

不同的处理器架构或操作系统会选择不同的内存一致性模型,应用开发者需要了解并遵循所选的模型规范,以确保程序正确性。

Simply put

Memory consistency model refers to the set of rules that govern how memory operations are performed and how they appear to be executed by a parallel computing system. It defines the order in which memory operations appear to execute in a multi-threaded environment.

The memory consistency model is important for ensuring correct synchronization between threads in a multi-threaded environment. It provides a way to reason about the ordering of memory operations and ensures that the results of one thread’s actions are visible to other threads in the correct order.

The design and implementation of a memory consistency model involves defining the set of rules that govern how memory operations are performed and how they appear to be executed by the system. This includes identifying the set of memory operations that need to be supported by the system, defining the order in which these operations can be executed, and implementing the rules in the system through hardware and software components.

Happened-before

In Java Memory Model (JMM), happened-before is a concept that defines the ordering of memory operations in a multi-threaded environment. It is a relationship between two events where one event happens before another event.

The happened-before relationship is established by the following:

  1. Program order: Operations in a single thread are executed in program order, which means that the order of execution is the same as the order in which they appear in the code.

  2. Volatile variables: If a write operation to a volatile variable happens before a read operation of the same variable in another thread, then the write operation is said to have happened before the read operation.

  3. Synchronized blocks: If a thread executes a synchronized block and then another thread executes a synchronized block on the same monitor, then the first thread’s actions are said to have happened before the second thread’s actions.

  4. Thread start: The start of a thread happens-before any actions in the started thread.

  5. Thread join: Any actions in a thread happen-before any other thread calling join() on that thread.

The happened-before relationship is important for ensuring correct synchronization between threads in a multi-threaded environment. It provides a way to reason about the ordering of memory operations and ensures that the results of one thread’s actions are visible to other threads in the correct order.

你可能感兴趣的:(Java,Basic,knowledge,java)