First, we should refer to what memory model. Book are defined: under specific operating protocols, during read and write access to a specific memory and cache abstraction.
Java memory model provides:
1, all the variables stored in the main memory;
2, each thread has its own working memory, and the operation of the variables are carried out in the working memory;
3, between different threads can not directly access the working memory of another variable, it can only be passed through in order to access main memory.
Java threads, working memory, main memory relationship as shown below:
DETAILED variable from main memory into the working memory, and implementation details from the main memory back to the working memory, the operation performed by the following eight atoms of:
lock: acting on the main memory variable, the variable is identified as a thread exclusive state
unlock: variable acting on the main memory, the exclusive state is released
read: acting on the main memory variables, is copied into a working memory
load: the role of the working memory variable, put a copy of the value of the variable in the working memory
use: the role of the working memory variable, the value is passed to the execution engine
asign: the role of the working memory variables, the execution engine of the value assigned to the variables in the working memory
store: the role of the working variables in memory, the value is passed to main memory
write: acting on the main memory variables, the value returned from the working memory into the main memory variable
Also made of the above-mentioned eight operations require some details, such as read / load, store / write must be paired, lock variable is not performed unlock operation can not be performed.
Key planning, interview questions commonly encountered here is the volatile keyword for interpretation.
volatile keyword
This keyword is modified variables have two effects: 1, to ensure visibility between threads; 2, to prevent instruction reordering
For the realization of 1, and use it to ensure the load must be adjacent call, that is, to use this variable, you must first perform read / load, so every time to get the latest variable value; it must ensure asign adjacent to the store to call after that in the working memory variables changed, certainly it will first be synchronized to the main memory. In this way, volatile keyword of visibility. As prevent instruction reordering, or venue " in-depth understanding of the Java Virtual Machine ," a book club, limited Pindao level, do not say here.
From another perspective analysis, Java memory model is centered on how to deal with atomic concurrent process, visibility, ordering to build.
Atomicity: eight atomicity operations, as well as the synchronized (lock / unlock is not directly open to the user, the synchronized with the call by monitorenter monitorexit instruction lock / unlock operation)
Visibility: volatile, synchronized, final three keywords are to achieve visibility in different ways
Ordering: volatile, synchronized these two keywords to ensure orderly, as well as (happens-before) to ensure that the principle of first occurrence of implicit default ordering
If the operation procedure A occurs before B, then A B produce the same effects can be observed: happens-before principle occurs first, the principle is the first occurrence of the following expression in plain language to talk. So the question is, what are the principles it first happened? Also there are eight, as follows:
Program sequence rules: the same thread are executed in the order code
Tube-lock rule: For lock with a lock, unlock first occurred in the back, that will unlock the lock
volatile variable rules: Write operations on a volatile variable first occurred in the back of the variables read, that reading will be finished
Thread start rule: a thread start () method in advance of any action takes place in this thread
Thread terminates rules: All actions take place ahead of a thread in the thread termination detection
Thread break the rules: a call to a thread interrupt () method to the first occurrence of a thread break detection Thread.interrpted ()
End of subject rule: initial completion method first occurred in the finalize ()
Transitive: As the name suggests, A occurred in the preceding B, B occurred in the preceding C, then A must occur in the preceding C
to sum up
Java memory model on these basic contents, if you have mastered it, the non-basic tier Internet companies can respond freely the (first-tier Internet companies because I did not go Pindao> <).