java memory model and thread
The actions of a thread are determined by the semantics of the statements in the methods that it executes.
Logically, these statements are executed in the order the statements are writtenan order known as program order.
However, the values of any variables read by the thread are determined by the memory model.
The memory model defines the set of allowed values that can be returned when a variable is read.
In this context variables consist only of fields (both static and non-static) and array elements.
From a programmer's perspective this set of values should only consist of a single value: that value most recently written by some thread. However, as we shall see, in the absence of proper synchronization, the actual set of values available may consist of many different values.
多个线程共享一个变量,他们所能读到的值是由memory model确定的,jmm认为在这种情况下,变量允许读到的不是一个值,而是一个值的集合,这个集合中的任何值作为这个变量的值都是合法的。 但对于编程的人来说,显然应该是最近写入的值,要达到这种效果需要借助合适的同步机制。否则jmm只能保证顺序一致性(The Happens-Before Relationship)
浙公网安备 33010602011771号