In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
1. Java memory model
Java Memory Modle, referred to as JMM, Chinese name Java memory model, it is an abstract concept, used to describe or regulate the way to access memory variables. Because the operating system and hardware of each computer are different, and the way and mechanism may be different, the Java memory model is used to shield (adapt) various differences, so as to achieve the consistent effect of accessing each platform. This is also one of the important reasons why Java boasts the platform.
Main memory: Java memory stipulates that all variables are stored in main memory (Main Memory), each thread has its own local memory (working memory), and local memory holds some variables in main memory. The specific access methods are as follows:
The way JMM works
Lock locking: in order to ensure the thread safety of accessing main memory variables, lock processing is usually added before access; read read: read a variable from main memory into working memory; load load: load variables read by read into a copy of variables in working memory; use use: threads can use variables in their working memory at this time; assign assignment: assign processed variables to variables in working memory Store storage: store the variables in the working memory in the main memory and store them in the way of creating a new new variable; write write: assign the reference of the new variable that exists in store to the variable being processed; unload unlock: all the work is done, and finally unlock and release resources. 2. Three characteristics of Java memory model
1. Atomicity (Atomicity)
Atomicity here is like atomicity in database transactions, where one or more operations either succeed or fail (none at all).
Int a = 1 / 5
The Java memory model only guarantees the atomicity of a single operation, such as int a = 1 above; it is a single operation, so it is atomic. At the bottom level, the await + operation is divided into three operations: 1), reading the value of a to the temporary variable; 2), adding 1 to the value of the temporary variable a; and 3) assigning the value of the added operation to a. Each operation is atomic, but the Java memory model does not guarantee the overall atomicity of multiple operations under multithreading, because it does not know how many operations are in the whole. If users want to achieve the overall atomicity of multiple operations, they need to synchronous the code blocks of the response, such as using locked synchronized or unlocked CAS.
two。 Visibility (Visibility)
The visibility here is memory visibility.
As shown in the figure above, thread 1 and thread 2 access variables in shared memory (main memory) without synchronization, for example, both threads add one to the variable a. Suppose thread 1 first gets the value of the variable an in the main memory, and then thread 2 gets the value of the variable an in the main memory, and then the value of an in the working memory is 1, and then they each add the value of a to the working memory, and then the value of the variable an in the working memory is 2, and then the two threads refresh the value to the main memory, and the final result is that the value of the variable an in the main memory is 2. Although the whole operation of adding 1 to the value of a has been done twice, because the operations between threads are isolated from each other, it is impossible to perceive the subsequent changes in the value of memory variables by default, so they cannot access the latest variable values in memory. This is the problem of memory feasibility.
How to solve the problem of memory visibility?
Synchronize threads entering the critical section (such as synchronized), only one thread can access the resources in the critical area at the same time; use the volatile keyword to ensure memory visibility, it can ensure that all threads accessing the resources in the critical area can always see the latest value of the shared resources; CAS is unlocked.
3. Ordering (Ordering)
All operations within the thread are orderly, that is, the order in which the program is executed is in the order of the code. For example, the following example:
Int a = 1nint b = 2nint c = a + b
The in-thread program executes int a = 1;, then int b = 2, and finally int c = a + b;.
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.