Sometimes the optimization of compilers and processors will cause runtime to be different from what we imagined. For this reason, Java has imposed some restrictions on compilers and processors. The JAVA memory model (JMM) abstracts these out so that there is no need to consider so many underlying details when writing code, and ensures that "as long as you follow the JMM rules to write a program, the running result must be correct."
Abstract structure of JMM
In Java, all instances and static variables are stored in heap memory, which can be shared between threads, and this part is also called shared variables . Local variables, method definition parameters, and exception handling parameters are on the stack, and the stack memory is not shared among threads.
However, due to the optimization of compiler and processor, there will be visibility problems with shared variables. For example, in multi-processors, threads can be executed on different processors, and the cache inconsistent between processors will cause visibility problems with shared variables . It is possible that two threads see different values of the same variable.
JMM abstracts the optimizations made by these hardware into that each thread has a local memory. When you need to read and write shared variables, copy a copy from the main memory to local memory. When writing shared variables, write them to the local memory first, and then refresh them to the main memory at some time in the future. When the shared variable is read again, it will only be read from local memory.
In this way, communication between threads requires two steps:
Write thread: refresh the local memory and read the thread: read the updated value from the main memory
In this way, there is a delay between writing and reading: When will the local memory be refreshed to the main memory? This leads to visibility problems, and different threads may see different shared variables.
happens-before
literally happens-before means "before happening before this". This is the rule that Java formulates on the order of program execution, and the synchronization must be followed. In this way, programmers only need to write out the correct synchronous program, and happens-before ensures that the running results will not be wrong.
A happens-before B not only means that A is executed before B, but also means that A's execution result is visible to B, which ensures visibility.
A happens-before B, A does not have to be executed before B. If AB alternates and the execution results are still correct, the compiler and processor are allowed to optimize reorder. So as long as the program results are correct, there is no problem with how the compiler and processor optimize and reorder it, and it is all good.
happens-before rules
Program sequence rules: In a thread, the operation lock rules after the previous operation happens-before: For the same lock, unlock happens-before and lock the volatile domain rules: Write the volatile variable, and read any of the volatile variables after happens-before. Transitiveness of the operation: A happens-before B, B happens-before C, then A happens-before C start() rules: If thread A executes ThreadB.start() then ThreadB.start() happens-before Any operation join() rules in thread B: If thread A executes ThreadB.join(), then all operations in thread B happens-before ThreadB.join() happens-before
The following example helps to understand happens-before
double pi = 3.14; //Adouble r = 1.0; //Bdouble area = pi * r *r; //C
Here are three happens-before relationships, rules 1 and 2 are program order rules, and rules 3 are derived from transitive rules:
A happens-before BB happens-before CA happens-before C
C depends on A and B, but neither A nor B depend on it. So even if A and B are reordered, the execution results will not change. In this reordering, JMM is running.
The following two execution sequences are correct.
The above is all the content we have compiled for you about learning Java memory model JMM. For more questions, please leave a message below to discuss. Thank you for your support for Wulin.com.