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

Knowledge Point explanation of Java memory Model

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "explaining the knowledge points of the Java memory model". In the operation of the actual case, 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!

Memory model (memory model)

The memory model describes the relationship between variables (instance fields, static fields and array elements) in a program, as well as low-level details such as storing variables in memory and fetching variables from memory in a real computer system.

The processor architecture between different platforms will directly affect the structure of the memory model.

In C or C++, you can use memory models under different operating platforms to write concurrent programs. However, what this brings to developers is the higher learning cost. In contrast, Java makes use of the advantages of its own virtual machine, so that the memory model is not tied to the specific processor architecture, and the cross-platform is really realized through the Java memory model. (memory models will also be different for different jvm such as hotspot jvm, jrockit, etc.)

Characteristics of the memory model:

A, Visibility visibility (multi-core, multi-thread data sharing)

B, Ordering ordering (operations on memory should be orderly)

Java memory Model (java memory model)

According to the instructions in Java Language Specification, there is a main memory (Main Memory or Java Heap Memory) in the jvm system, and all variables in Java are stored in main memory and shared for all threads.

Each thread has its own working memory (Working Memory). What is saved in the working memory is the copy of some variables in the main memory. The operation of the thread on all variables is carried out in the working memory, and the threads can not access each other directly.

Among them, most of the variables in working memory are stored in the processor cache under multi-core processors, and the cache is not visible when it does not pass through memory.

How does jmm reflect Visibility?

In jmm, by modifying variable values through concurrent threads, thread variables must be synchronized back to main memory before other threads can access them.

How does jmm reflect Ordering?

The access order of memory is guaranteed through the synchronization mechanism provided by Java or the volatile keyword.

Cache consistency (cache coherency)

What is cache consistency?

It is a cache structure for managing multiprocessor systems, which can ensure that data will not be lost or repeated in the transfer from cache to memory. (from wikipedia)

Take an example to understand:

If a processor has an updated variable value in its cache but has not been written to main memory, other processors may not see the updated value.

How to solve cache consistency?

A, sequential consistency model:

A processor is required to propagate the value of the changed variable immediately and ensure that the value is accepted by all processors before other instructions can be executed.

B, release the consistency model: (similar to jmm cache coherency)

Allows the processor to delay the value of the changed variable until the lock is released.

Cache consistency Model of Java memory Model-"happens-before ordering (sort in advance)"

Sample programs in general:

X = 0; y = 0; I = 0; j = 0; / / thread A y = 1; x = 1; / / thread B i = x; j = y

In the above program, what would be the value of iGraine j if thread Aline B was running without guarantee?

The answer is, not sure. Java synchronization mechanism is not used here, so the orderliness and visibility of Java memory model cannot be guaranteed. How can happens-before ordering (sort first) avoid this situation? The sorting principle has been achieved:

A, in the program order, each operation in the thread occurs before each operation that will occur after the current operation.

B, the unlocking of the object monitor occurs before the thread waiting to acquire the object lock.

C, the variable write operation modified by the volitile keyword occurs before the variable is read.

D, the Thread.start () call to a thread occurs before all operations in the started thread.

E, all operations in a thread occur before all other threads returned successfully from this thread's Thread.join ().

To implement the happends-before ordering principle, Java and the tools provided by JDK:

A, synchronized keyword

B, volatile keyword

C, final variable

D, java.util.concurrent.locks package (since jdk 1.5)

E, java.util.concurrent.atmoic package (since jdk 1.5)

An example of using happens-before ordering:

1) acquire the lock of the object monitor (lock)

(2) clear the working memory data and copy variables from the main memory to the current working memory, that is, synchronous data (read and load)

(3) execute code to change the value of shared variables (use and assign)

(4) flush the working memory data back to main memory (store and write)

(5) release the lock of the object monitor (unlock)

Note: the two steps of 4 and 5 are carried out at the same time.

The core here is the second step, which synchronizes the main memory, that is, the result of the variable changes made by the previous thread, which can be known by the current thread! (take advantage of the happens-before ordering principle)

Compare with the previous example

If multiple threads execute an unlocked segment of code at the same time, it is very likely that one thread has changed the value of the variable, but other threads cannot see the change and still operate on the value of the old variable. resulting in unpredictable results.

This is the end of the introduction to the knowledge points of the Java memory model. Thank you for your 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.

Share To

Development

Wechat

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

12
Report