java memory model and multi-threaded environment

https://www.infoq.cn/profile/1278512

runtime data area
http://coding-geek.com/jvm-memory-model/

A reordering sequential consistency and

Second, the three synchronization primitives (lock, volatile, final) semantic memory

Third, rules and reordering implemented in a processor

Fourth, the design of java memory model, and its relationship with the processor memory model and sequential consistency memory model.

runtime data areas

http://coding-geek.com/jvm-memory-model/

Communication between multiple threads: Shared Memory

Synchronization: control operation relative order between different threads

Memory visibility problems

JMM decides when a thread writes to shared variables visible to another thread

Thread private local memory
shared between threads main memory main memory

local memory and main memory are abstractions.

local memory copy main memory shared copy of the variable.

Split into read, write two operations.

Reordering

Compiling discouraged sort
instruction reordering
memory reordering

Wherein the reordering of instructions and a processor memory is reordered reordering
processor belonging to a reordering of the operating system and hardware problems, in order to maintain the specification JMM, sorted and compiled discouraged memory barrier maintained JMM

Memory barriers can solve the problem of reordering processor, is realized inside jvm

Programmers focus to compile and sort discouraged memory barrier is enough. No need to continue deep into the processor reordering.

JMM specification of abstract principle happens-before

Note that with a happens-before relationship between the two operations, does not mean that a pre-operation must be performed prior to an operation after! Before it happens-before requires only one operation (executed result) after the operation of a visible and sequenced previous operation before the second operation (the first is visible to and ordered before the second). happens- before the definition is very delicate, the text will specify why so happens-before defined.

After the operation requires only a pair of front operation visible.

Sequential consistency memory model (abstraction)

JMM implement this concept

First order of consistency this concept alone carry out:

JMM does not guarantee the long reading of 64-bit type and double-type variable / write operations are atomic, the sequential consistency model to ensure that all of the memory read / write operations are atomic.

volatile

Modified volatile domain (variable), happens-before reading of the domain (variable)

Monitor lock semantics determines the execution of a critical section of code atomic. This means that even a 64-bit long and double-type variable, as long as it is a volatile variable, the variable will be read atomic. If a plurality of operations or similar volatile ++ volatile compound such operations, which do not have atomic whole.

Constructor, the singleton (lazy type), multi-threaded environment

Constructors started

写final域

Constructor execution ends

Thread A
obj = new new ObjectA ()

只有当构造函数的 jvm stack -> frame(栈帧)弹出, obj才会被赋值,否则obj保持null不变
并且加锁了, 在锁释放前local memory 中的数据会被刷新到 main memory 

Thread B
obj.execMethod ()

The impact of volatile and final reordering, the impact on the visibility of the lock memory (called memory visibility of local memory is flushed to the variables in the main memory)

JMM comply happens-before principle
but allows reordering
final and volatile effect on the reordering of
the impact on the visibility of the lock memory (called memory visibility of local memory is flushed to the variables in the main memory)

Single thread local memory in writing of variables not visible to outside
main memory is externally visible variables

When a thread releases the lock, JMM will corresponding to the thread local memory shared variables flushed to main memory.

Reproduced in: https: //www.jianshu.com/p/f3ff5e5c4442

Guess you like

Origin blog.csdn.net/weixin_33742618/article/details/91070084