Order reordering
A visit to main memory typically costs hundreds of cycles of hardware. Processors are able to reduce the cost of memory latency from the order of magnitude by caching (caching) These caches order to reorder the pending memory operations for performance. In other words, a program's read and write operations do not necessarily follow the order in which it requires the processor.
Reorder the background
We know that the frequency of modern CPUs is getting higher and the number of interactions with the cache is increasing. Cache wait is generated when the CPU is calculated much faster than the access cache, and excessive cache wait causes a performance bottleneck.
In this case, most architectures (including X86) employ a solution that caches shards, and a cache is divided into unrelated multiple slots (logical storage unit, aka Memory Bank or Cache bank), and the CPU can choose between multiple idle Bank for access. This SMP design significantly improves the CPU's parallel processing capability and avoids the cache access bottleneck.
Division of Memory Bank
The General Memory Bank is broken down by the cache address. For example even adress 0x12345000 to bank 0, odd address 0x12345100 to Bank1.
Types of reordering
The compilation period is re-queued. When compiling the source code, the compiler re-sorts the instructions based on the parsing of the context, which is more appropriate for parallel execution of the CPU.
Run-time reflow, the CPU in the execution process, dynamic analysis of the performance of dependent components, order reordering optimization.
The Java language specification specifies that sequential semantics are maintained within the JVM thread, meaning that the order of execution of the instructions may not be consistent with the order of the Code as long as the final result of the program equals the result of its strict sequential environment. This process is done by reordering called directives. The meaning of command reordering is that the JVM is able to properly reorder machine instructions based on the characteristics of the processor (multi-level cache system, multi-core processor, etc.), so that the machine instruction is more consistent with the performance of the CPU, and the machine is maximized.
The simplest model of the program executes in the order in which the instructions appear, which is independent of the CPU of the executing instruction, maximizing the portability of the instructions. The technical terminology of this model is called sequential conformance model. However, both modern computer systems and processor architectures do not guarantee this (because human designations are not always guaranteed to conform to the CPU processing characteristics).
Let's look at one of the most classic cases.
Package xylz.study.concurrency.atomic;
public class Reorderingdemo{
static int x = 0, y = 0, a = 0, b = 0;
public static void Main (string[] args) throws Exception{
for (int i = 0; i <; i++){
x=y=a=b=0;
Thread one = new Thread (){
public void Run (){
A = 1;
x = b;
}
};
Thread of both = new Thread (){
public void Run (){
b = 1;
y = A;
}
};
One.start ();
Two.start ();
One.join ();
Two.join ();
SYSTEM.OUT.PRINTLN (x + "" + y);
}
}
}
In this example one/two two thread modification zones x,y,a,b four variables, which can be obtained (0 1) or (1 0) or (1 1) in the case of 100 executions. In fact, according to the specifications of the JVM and the characteristics of the CPU is likely to get (0 0). Of course, the above code is not necessarily available (0 0), because the operation inside the run () is too simple and may take less time than starting a thread, so the above example is hard to come by (0,0). But it does exist on modern CPUs and JVMs. Because the action inside the run () is irrelevant to the result, the instructions in the command can be reordered, even if it is executed in the order of the program, and it takes time to refresh the data to main memory. It is assumed that the x=0 is normal in accordance with A=1;x=b;b=1;y=a, although A=1 was executed before Y=a, but because the thread one did not have time to write the data 1 back to main memory after the a=1 was completed (this time the data is inside the stack of the thread a), The data A that thread B gets from main memory may still be 0 (obviously an out-of-date data, but it is possible), so a data error occurs.
The result of the data is uncertain when the two threads are executed alternately, and the result of the data is even more uncertain in the case of high machine pressure and concurrent execution of multi-core CPUs.
Happens-before Law
The memory structure of Java is as follows
This also works well if you do not share data between multiple threads, but if you want to share data between multiple threads, then these disorderly execution, the data in the register of the behavior will cause the program behavior uncertainty, now the processor is a multicore era, these problems will be more serious, each thread has its own working memory, Multiple threads share the main memory,
If the data is shared, when will sync to main memory let someone else's thread read the data? This is also uncertain, if not to be consistent, then expensive, this will sacrifice the performance of the processor, so now the processor will sacrifice storage consistency in exchange for performance, if the program to ensure consistency when sharing data, the processor usually provides some level instructions, which can help the programmer to implement, But a variety of processors are different, if you want to make the program can cross the platform is not possible, what to do?
Using Java, which is shielded by JMM (Java memeory Model Action), we just have to use the consistency guarantee with JMM's rules, so what's the guarantee for JMM? the definition of JMM is described in the form of action, the so-called action, including the variable read and write, monitor lock and release lock, thread start and stitching, this is the legendary happen before, want to a action see B action results,B and A must satisfythe H appen before relationship , the happen before law is as follows:
1, procedure Order rule, if a must occur before B, then happen before,
2, the law of the monitor, the unlocking of a monitor must occur in the subsequent lock on the same monitor
3, volatie variable rule: Write volatile variable must occur before subsequent read to it
4, Thread start law: Thread.Start must occur in the course of the action
5, thread termination rule: Any action in the thread must occur before the action in parentheses (the other thread detects that the thread has been terminated, returns successfully from the Thread.Join call, and Thread.isalive () returns false)
6, Interrupt rule: One thread calls another thread's interrupt must occur on another path to discover the interrupt.
7, the rule of termination: The end of an object's constructor must occur before the object's finalizer
8, transitivity: A occurs before B, B occurs before C, a must occur before C.
transferred from: http://blog.163.com/javaee_chen/blog/static/179195077201131382128499/
Order reordering and Happens-before Law essays