In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Java memory model main memory and working memory interaction analysis," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "Java memory model main memory and working memory interaction analysis" bar!
I. Foreword
1. Variable
The main goal of the Java memory model is to define access rules for individual variables (non-local variables and method parameters) in a program, i.e., the low-level details of storing and retrieving variables from memory in a virtual machine.
Variables here are different from variables in Java programming. They include instance fields, static fields, and elements that make up array objects, but they do not include local variables and method parameters, because these are private to threads and will not be shared. To achieve better execution performance, the Java memory model does not restrict the execution engine from using processor-specific registers or caches to interact with main memory, nor does it restrict the compiler from making optimizations such as adjusting code execution order.
2. Discussion level
The main memory, working memory and Java heap, stack, method area in Java memory area mentioned in this article are not the same level of memory division, and the two basically have nothing to do with each other;
If you must reluctantly correspond, then from the definition of variables, main memory, and working memory, main memory mainly corresponds to the part of the object instance data in the Java heap, while working memory corresponds to part of the virtual machine stack.
At a lower level, main memory corresponds directly to physical hardware memory, and for better speed, a virtual machine may prioritize working memory in registers and caches because it is working memory that is accessed primarily by programs running.
II. Mandatory norms
The Java memory model specifies that all variables are stored in main memory.
Each thread also has its own working memory. The working memory of the thread stores a copy of the main memory of the variable used by the thread. All operations (reading, assignment, etc.) of the thread on the variable must be carried out in the working memory, and the variable in the main memory cannot be read or written directly. Different threads cannot directly access variables in each other's working memory, and the transfer of values of variables between threads needs to be completed through main memory.
III. Inter-memory interaction
About the specific interaction protocol between main memory and working memory, that is, how a variable is copied from main memory to working memory, how to synchronize from working memory back to main memory and other implementation details, Java memory model defines the following eight operations to complete, virtual machine implementation must ensure that each of the operations mentioned below are atomic, inseparable.
lock: acts on main memory variables, identifying a variable as a thread-exclusive state
unlock: A variable that acts on main memory to release a locked variable before it can be locked by another thread.
read: A variable applied to main memory that transfers the value of a variable from main memory to the thread's working memory for use in subsequent load actions
load: A variable that acts on working memory and places the variable value obtained from main memory by the read operation into a copy of the variable in working memory
use: A variable that acts on working memory to pass the value of a variable in working memory to the execution engine, which will be executed whenever the virtual machine encounters a bytecode instruction that requires the value of the variable
assign: A variable that acts on working memory, assigning a value received from the execution engine to a variable in working memory, and performs this operation whenever the virtual machine encounters a bytecode instruction that assigns a variable
store: A variable that acts on working memory to transfer the value of a variable from working memory to main memory for use in subsequent write operations
write: A variable that acts on main memory and places the value of the variable obtained by the store operation from working memory into a variable in main memory
To copy a variable from main memory to working memory, read and load operations are performed sequentially; to synchronize a variable from working memory back to main memory, store and write operations are performed sequentially. Note that the Java memory model only requires that the above two operations must be executed in sequence, and there is no guarantee that they are executed continuously, that is, between read and load, between store and write, other instructions can be inserted.
At this point, I believe that we have a deeper understanding of "Java memory model of main memory and working memory interaction analysis," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.