In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the thread memory model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Thinking Transformation of memory Model Diagram
JVM defines the virtual memory model of Java, which is different from the virtual memory that divides the physical memory into different areas, which directly maps the physical memory.
Generally speaking, jvm generally divides memory into stacks and heaps, which are used to store static methods and static variables, and heaps to store objects and ordinary variables.
But from a threading point of view, the memory model will look like the following figure
In this model, variables are in main memory, threads have their own working memory, and there is no interference with each other.
The working memory realizes the exchange and sharing of variable data through the operation with the main memory. Thread working memory is isolated from each other. In this way, when each thread works, it will not affect the working data of other threads.
two。 Instance parsing thread memory model
As shown in the figure above, on the left is a simple ticketing program, and on the right is the memory model loaded by the program. When the program is loaded into the method area through the class loader, the thread of the main method established in the stack creates the variable of Tiecket t, and the member variable tickets and method of the object are established in the heap.
When window 1 and window 1 threads are created in the main method, each thread has an independent stack memory space, and the stack memory shares the same variable tickets
3. Operation between working memory and main memory (single thread)
There are 8 operations in the two blocks of memory.
(lock-unlock) lock marks a main memory variable as thread exclusive, and unlock releases the exclusive variable
(read-load) read reads the variables of main memory into CPU, and load operation stores the variables of read into working memory, which must appear in pairs.
(use-assign) use passes variables in working memory to the executing code, which is required when the code needs to use the bytecode of the variable value. Assign assignment operation, which assigns the received variable to working memory when the assignment instruction appears in the code
(store-write) store passes variables from working memory back to main memory, but it is only by passing that the write operation writes values to main memory. And these two are bound to show up in pairs.
Read load; store write can only operate in pairs. It cannot be read-only or not used, and only returns but does not store.
Threads are not allowed to discard assign operations, used variables must be returned to main memory, and unassign variables are not allowed to be written back to main memory from working memory.
Variables can only be created from main memory, and uninitialized variable threads cannot load or assign
Variables can only be lock by one thread, and can be lock many times, and the corresponding number of unlock must be executed before it is released.
Threads can only unlock variables of their own lock, variables that are not lock cannot execute unlock, and variables of other threads' lock are not allowed to unlock.
The lock variable operation will empty the variables in the working memory. When using this variable, you need to re-load and assign when using this variable.
Before unlock, variables are rewritten to main memory
This is the end of "what is the Thread memory Model". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.