Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master Java memory model and volatile keywords

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "Java memory model and how to master volatile keywords", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java memory model and volatile keywords how to master" it!

Java memory Model (JMM):

Related concepts: 1) in imperative programming, there are two communication mechanisms between threads: shared memory and message passing. 2) the concurrency of java adopts the shared memory model: implicit communication is carried out by reading / writing common states in memory. Concept: communication between Java threads is controlled by the java memory model, and JMM determines when writes by one thread to shared variables are visible to another thread. Description: 1 > shared variables between threads are stored in main memory, each thread has a private working memory, and a copy of the thread's read / write shared variables is stored in the working memory. 2 > working memory is an abstract concept of JMM and does not really exist. It covers caching, write buffers, registers, and other hardware and compiler optimizations. 3 > all operations on variables (reading, assigning, etc.) by the thread must be carried out in working memory, rather than directly reading and writing variables in main memory. 4 > there is no direct access to variables in each other's working memory between different threads, and the transfer of variable values between threads needs to be completed through the main memory.

Volatile keyword:

Related concept: cache line: the minimum unit of storage that can be allocated in the cache. L1 cache: internal cache. L2 cache: external cache. Principle: 1) in order to improve the processing speed, the processor does not communicate directly with the memory, but first reads the data in the system memory to the cache (L1, L2) and then operates, but after the operation is completed, the processor does not know when to write the data back to memory. 2) when writing to a variable modified by volatile, JVM will send an instruction with a Lock prefix to the processor, write the data of the cache line (that is, the working memory in JMM) of the variable back to system memory, and invalidate the data in other CPU that has the memory address cached. Add: 1 > when writing (assigning) to volatile-modified variables, we will find an instruction prefixed with Lock in the assembly instructions generated by the JIT compiler. 2 > instructions prefixed with Lock cause two things under multi-core processors: ① writes the data of the current processor cache line back to system memory ②, which makes the data cached in other CPU invalid. Volatile memory primitive: when reading a volatile variable, JMM will invalidate the working memory corresponding to the thread, and the thread will then read the shared variable from the main memory. When writing a volatile variable, JMM flushes the value of the shared variable in the working memory corresponding to the thread to the main memory. That is, 1 sets the data in the local memory to be invalid, 2 copies the data from the main memory to the local memory, 3 operates in the local memory, and 4 refreshes the data in the local memory to the main memory after the operation is completed. The whole looks as if it is operating directly in main memory. Note: if a variable decorated with volatile is changed by one thread, then other threads will immediately perceive it, and each thread gets the latest value of the variable, and accessing the variable modified by volatile looks like reading and writing directly in memory. Feature: visibility: when reading a volatile variable, (any thread) can always see the last write to the volatile variable. Atomicity: reading / writing to a volatile variable is atomic, but composite operations like volatile++ are not atomic. Advantages: does not cause thread context switching volatile and synchronized comparison: 1) keyword volatile can only modify variables, synchronized can modify code blocks, method 2) volatile can not guarantee atomicity, synchronized guarantees atomicity: volatile can guarantee data visibility, but can not guarantee atomicity, so volatile solves the visibility of variables between multithreads. Synchronized ensures atomicity and visibility (synchronized synchronizes data in private and public memory), so synchronized addresses the synchronization of accessing resources between multiple threads.

Reorder:

Description: when executing a program, compilers and processors often reorder instructions in order to improve performance. Reordering can be divided into two types: 1) compiler reordering: the compiler can rearrange the execution order of statements without changing the semantics of single-threaded programs. 2) processor reordering: 1 > instruction-level parallel reordering: modern processors use instruction-level parallel technology to overlap multiple instructions. If there is no data dependency, the processor can change the execution order of the statements corresponding to the machine instructions. 2 > reordering of memory systems: because the processor uses caches and read / write buffers, load and storage operations may appear to be out of order. JMM how to achieve volatile write / read memory semantics: 1) JMM volatile reordering rules for compilers: conditions for reordering between two operations: 1 > when the first operation is volatile read, no matter what the second operation is, it cannot be reordered. This rule ensures that operations after volatile read are not reordered by the compiler before volatile read. 2 > when the second operation is volatile write, no matter what the first operation is, it cannot be reordered. This rule ensures that operations before volatile writing are not reordered by the compiler after volatile writing. 3 > when the first operation is volatile write and the second operation is volatile read, it cannot be reordered. It can be concluded from the above three points that two volatile variable operations cannot be reordered. 2) in order to implement the memory semantics of volatile, when generating bytecode, the compiler will insert a memory barrier in the instruction sequence to prohibit the reordering of specific types of processors. (memory barrier: flushes the shared variable values of the previous operation to main memory.) Thank you for reading, the above is the content of "how to master Java memory model and volatile keywords". After the study of this article, I believe you have a deeper understanding of how to master Java memory model and volatile keywords, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report