Reference post: http://blog.csdn.net/suifeng3051/article/details/52611310
Http://www.cnblogs.com/nexiyi/p/java_memory_model_and_thread.html
Http://www.cnblogs.com/dolphin0520/p/3613043.html
I. Partitioning of the Java memory area
Since Java programs are given to the JVM, we are actually referring to the JVM memory partitioning when we talk about the Java Memory Area analysis.
According to the Java Virtual Machine specification, the runtime data area typically includes these parts: program Counter Register, Java stack (VM stack), local method Stack (Native), method area ( Method area), heap.
As shown, the runtime data area in the JVM should include these parts. Although it is stipulated in the JVM specification that the data area should include these parts when the program is run during execution, the different virtual machine vendors can have different implementations as to how the implementation is not specified.
1. Program counter: Used to indicate which command to execute
Because in the JVM, multithreading is a thread that takes turns to get CPU execution time, so at any given moment, a CPU core executes only the instructions in one thread
Therefore, in order to be able to enable each thread to resume the program execution location before the switch , each thread needs to have its own independent program counter, and can not interfere with each other, otherwise it will affect the normal execution order of the program.
So you can say that the program counters are the private ones of each thread.
2.Java stack: Java stack is a memory model for Java method execution
The Java stack contains:
1. Local variable table (local variable in method) 2. Operand stack (all calculation procedures in the program are done with the help of the operand stack)
3. Reference to run a constant pool (reference to run-time constant) 4. Method return address (when a method finishes executing, to return to the place where it was previously called) 5. Additional Information
Because the methods that each thread is executing may be different, each thread will have its own Java stack that does not interfere
3. Local method Stack (for performing local method service)
The local method stack is similar to the Java stack, except that the Java stack is for executing Java method servers, while the local method stack is the one that performs the local methods (Native method) service.
In the hotsopt virtual machine, the local method stack and the Java stack are directly merged
4. Heap
The heap in Java is used to store the object itself as well as an array (of course, the array references are stored in the Java stack). In addition, the heap is shared by all threads, and there is only one heap in the JVM
5. Method area
The method area, like a heap, is a zone that is shared by threads. In the method area, information is stored for each class (including the name of the class, method information, field information), static variables, constants, compiled code, and so on.
According to the above, it may be concluded that:
When we create a thread, it assigns the thread's unique stack space and heap space
Stack space is used to execute threads ' own methods
Heap Space stores objects copied from main memory, and variables created by this thread in the thread
Creating a variable in the thread will open up a piece of memory in the thread's own heap space (guess, this variable is also synchronized in main memory?). , or that the object was created in main memory)
Second, Java memory model
The Java memory model specifies that all variables are stored in main memory, that each thread has its own working memory , that all operations of the thread on the variable (read, assigned) must be made in working memory and not directly read and write to the variables Variables in the other's working memory cannot be accessed directly between different threads, and the transfer of variable values between threads needs to be done in main memory
Note: This memory is sensed as a thread allocation, including part of the stack space (used to manipulate variables) and part of the heap space (to store variables)
Note: Communication between threads
Thread communication refers to the mechanism by which the threads exchange information. In imperative programming, the communication mechanism between threads has two kinds of shared memory and message passing
Messaging: Typical message delivery in Java is Wait () and notify ()
Shared memory: Communicating through a shared object
From the point of view, if you want to communicate between thread A and thread B, you have to go through the following 2 steps:
1.首先,线程A把本地内存A中更新过的共享变量刷新到主内存中去。2. 然后,线程B到主内存中去读取线程A之前已更新过的共享变量。
Overall, these two steps are essentially thread a sending a message to thread B, and the communication process must go through main memory.
By default, the working memory between threads is not shared, that is, the a thread does not see the working memory of the B thread, and the B thread copies the variable x from main memory and then operates on that variable.
A is not seeing what B does to x, it must wait for B to flush the value of x back into memory, and thread A knows
After modifying the variable x with the volatile keyword, the principle of volatile save visibility is to refresh each time the variable is accessed, so each access is the latest version in main memory
Thread B copies a copy of the variable x from main memory, and when B operates on I, it immediately refreshes the updated X back into main memory, and when a thread reads the value of x, it refreshes the main memory and gets the latest value of X.
Finally, it can be understood that the thread is manipulating object X on main memory ( not really, or having to operate on the thread's own workspace, but with this effect ) and that the threads are visible to the X object,
Java memory model JMM simple analysis