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 talk about Java memory Model in Java concurrent programming

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The content of this article mainly focuses on how to talk about the Java memory model in Java concurrent programming. the content of the article is clear and clear, which is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Physical computer concurrency problem

Before introducing the Java memory model, let's take a brief look at the concurrency problems in physical computers. Due to the difference of several orders of magnitude between the operation speed of the processor and the storage setting, so modern computers add a cache whose read and write speed is as close as possible to the processor as a buffer between memory and processor: copy the data needed for the operation into the cache, so that the operation can be performed quickly, and then synchronize from the cache to memory when the operation is over, so that the processor does not have to wait for slow memory read and write.

Cache-based storage interaction introduces a new problem: cache consistency. In a multiprocessor system, each processor has its own cache, and they share the same main memory, as shown in figure 2-1

When the computing tasks of multiple processors involve the same memory area, the respective cached data may be inconsistent. At this time, whose cached data will prevail when synchronizing back to the master. In order to solve the consistency problem, each processor needs to follow some protocols when accessing the cache and operate according to the protocol when reading and writing.

In addition to increasing the cache, in order to make full use of the computing units within the processor, the processor may optimize the input code out of order, and the processor will reorganize the results of the out-of-order execution after the calculation. it is guaranteed to be consistent with the results of sequential execution, but it does not guarantee that the order of each statement in the program is consistent with that in the input code, so If there is a computing task that relies on the intermediate results of another computing task, its ordering cannot be guaranteed by the order of the code.

Java memory model

Main memory and working memory

The Java memory model specifies that all variables are stored in main memory (where the main memory is the same as the physical computer's main memory name, but here is only part of the virtual machine memory), where the variables include instance fields, static fields, and elements that make up the array object, but not local variables and method parameters, because the latter is private to the thread. Each thread also has its own working memory (comparable to the processor's cache). A copy of the main memory copy of the variable used by the thread is kept in the thread's working memory. All operations on variables (read and write, etc.) must be in working memory and cannot directly read and write variables in main memory. Different threads can not directly access variables in each other's working memory, and the transfer of variable values between threads must be completed through the main memory. The interaction among thread, main memory and working memory is shown in figure 2-2.

Inter-memory interaction

The Java memory model defines eight operations to complete the implementation details of how a variable is copied from main memory to working memory and synchronized back to main memory from working memory. Virtual machines must be implemented to ensure that each operation is atomic and indivisible (double and long class variables allow exceptions).

Lock (locking): a variable that acts on main memory and identifies a variable as an exclusive state of a thread.

Unlock (unlock): a variable that acts on main memory and unlocks a variable in a locked state so that the unlocked variable can be locked by other threads.

Read (read): a variable acting on main memory that transfers the value of a variable from main memory to the thread's working memory for use by subsequent load actions.

Load (load): a variable that acts on main memory and places the value of the variable obtained by the read operation from main memory into a copy of the variable in working memory.

Use (use): a variable that acts on working memory and passes the value of a variable in working memory to the execution engine, which will be performed whenever the virtual machine encounters a bytecode instruction for the value of the variable to be used.

Assign (assignment): acts on a working memory variable, assigns a value received from the execution engine to the working memory variable, and performs this operation whenever the virtual machine encounters a bytecode instruction for a variable assignment.

Store (storage): acts on working memory variables, passing the value of a variable in working memory to main memory for use by subsequent write operations.

Write (write): acts on the main memory variable, putting the value of the variable obtained by the store operation from the working memory into the variable in the main memory.

The Java memory model states that the following rules must be met when performing the above eight basic operations:

Read and load, store and write must operate in pairs

Threads are not allowed to discard assign operations. Variables that change in working memory must be synchronized back to main memory.

Without assign operation, variables are not allowed to synchronize from working memory to main memory

New variables can only be born in main memory, and it is not allowed to use an uninitialized variable (load or assign) directly in working memory, that is, assign and load operations must be performed before real-time use and store of a variable.

Only one thread lock operation is allowed for a variable at a time, but the lock operation can be performed multiple times and the same number of unlock is executed before the variable is unlocked.

The lock operation will empty the working memory copy. Before the execution engine uses it, you need to re-execute the load or assign operation to initialize the value of the variable.

Without lock operations, unlock operations are not allowed. Unlock another thread variable is not allowed.

Before the unlock operation, you must store,write the operation and synchronize it back to the main memory.

Thank you for your reading. I believe you have some understanding of "how to talk about the Java memory model in Java concurrent programming". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

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

Development

Wechat

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

12
Report