Home > Back-end >  multithreading
multithreading

Time:05-09

Learning to multithreaded mentioned Java memory model this concept, said the Shared variables are assigned to the main memory, should not is a method of heap memory area? Confused understanding how should go,
Until understand is the stack memory modify the Shared variables inside, now learned multithreading is more a main memory and working memory should be how to understand,

CodePudding user response:


1) thread between the Shared variables stored in Main Memory (Main Memory)
2) each thread has a private Local Memory (Local) Memory, Local Memory is an abstraction of the JMM, not real, it covers the cache, the write buffer, registers, and other hardware and compiler optimizations, Local Memory to store the thread in read/write copy copy Shared variables,
3) from the lower level, main memory is the memory of hardware, and in order to obtain better running speed, the virtual machine and hardware system can make the working memory is stored in registers and priority cache,
4) threads in Java memory model of working memory (working memory) is the CPU registers and caching of abstract description, and within the JVM static storage model (JVM memory model) are just a physical partition for the memory, it is only limited in memory, and limited the JVM memory,

This article describe more clearly, you can refer to take a look at https://zhuanlan.zhihu.com/p/29881777

CodePudding user response:

First of all, memory is divided into static and non-static area first, Shared variables, namely by the static modified variable or method in static area, the JVM is initialized when loading a class has been added to the memory, rather than a static modified variable or method with no call, no thread to run, is not present in the memory, so a static variable or method, prior to the non-static variables or methods,

Second, when a program is running, that is, to create a thread to handle a request, will come out in the static section open up a space, this space is called working memory, belongs to the thread, have a heap memory store (object), the stack memory store (variable), the method of frame (a non-static modified method), and Shared variables JVM will create a copy to the working memory is used, but not in the in the static method is can modify the Shared variables in the static zone, wait for the thread to handle the request, dormancy or destruction, and will be back to write the Shared variables to the static area, the moment when a thread to handle will find Shared variable values changed,

Finally, we often say multi-threaded concurrent processing, can lead to uncontrolled Shared variables, namely the data is not accurate, if say Shared variables to 1, A and B two threads come almost at the same time request, or A isn't over, B come and request, to get copies of are 1, working memory is 1, and then what did you do for A and B minus 1, then write data back to zero, but in fact should be 1, because lost twice, so general for Shared variables will add A modifier for volatile, at the same time to modify the Shared variables lock code, and ensure that there is only one machine at the same time one thread to modify it, the meaning of the volatile modified Shared variables is, when the Shared variables changes, the JVM will notify each working memory, the Shared variables has changed, and will give the latest values to each working memory,