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

What is the stipulation of Java memory model

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

Share

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

This article introduces the relevant knowledge of "what is the stipulation of the Java 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!

First of all, you need to know what the memory model means. The definition in the book is the abstraction of the process of reading and writing access to specific memory and cache under a specific operating protocol.

As you can see, the memory model dictates how to read and write to the memory / cache. So the Java memory model is used to define the access rules of the program to Java memory. Furthermore, the Java memory model defines the access rules of variables (static variables, array object elements, etc., excluding local variables, method parameters) in the program.

Provisions of the Java memory model:

All variables are stored in main memory

Each thread has its own working memory, and the operation on variables is carried out in the working memory.

Different threads cannot directly access variables in each other's working memory, and access can only be passed through main memory.

The thread, working memory, and main memory relationships of Java are shown in the following figure:

The implementation details of the specific variables from the main memory to the working memory and from the working memory to the main memory are completed by the following eight atomic operations:

Lock: acts on the main memory variable to identify it as an exclusive state of a thread

Unlock: acting on the main memory variable to release the exclusive state

Read: acts on the main memory variable to copy the value to the working memory

Load: acts on a variable in working memory and puts the value in a copy of the variable in working memory

Use: a variable that acts on working memory and passes the value to the execution engine

Asign: a variable that acts on working memory and assigns values from the execution engine to variables in working memory

Store: a variable that acts on working memory and passes the value to main memory

Write: a variable that acts on the main memory to put the value returned in the working memory into the main memory variable

At the same time, there are some detailed requirements for the above eight operations, such as read/load, store/write must appear in pairs, variables that have not performed lock cannot perform unlock operations, and so on.

To make a point, the problem often encountered in the interview here is the interpretation of the volatile keyword.

Volatile keyword

The variables modified by this keyword have two effects: 1, to ensure visibility between threads; 2, to prevent instruction reordering

For the implementation of 1, it ensures that load and use must be called adjacent, that is, if you want to use this variable, you must first execute read/load, so that you can get the latest variable value every time; it also ensures that asign and store must be called adjacent to each other, that is, after changing the variable in the working memory, it must first be synchronized to the main memory. In this way, the volatile keyword enables visibility. As for preventing instruction reordering, let's move on to the book "in-depth understanding of the Java Virtual Machine." the poor road level is limited, so let's not talk about it here.

From another point of view, the Java memory model is built around how to deal with atomicity, visibility and order in the concurrency process.

Atomicity: eight atomic operations, and synchronized (lock/unlock operations that lock/unlock is not directly open to the user and synchronized calls through monitorenter and monitorexit instructions)

Visibility: the three keywords volatile, synchronized, and final all achieve visibility in different ways

Ordering: the keywords volatile and synchronized guarantee orderliness, while the principle of happens-before ensures implicit default orderliness.

Let's talk about the happens-before first occurrence principle, which is expressed in popular language: if operation An occurs before operation B, then the effect of A can also be observed. So the question is, what are the pre-emptive principles? There are also eight, as follows:

Program order rules: execute in the same thread in the order of the code

Pipe locking rule: for the same lock, unlock occurs first in the following lock, that is, lock only after unlock.

Volatile variable rule: the write operation to a volatile variable occurs first in the subsequent read operation on the variable, that is, it will not be read until it is finished.

Thread startup rule: the start () method of a thread occurs first in any action of this thread

Thread termination rule: all actions of a thread occur first in the termination detection of the thread

Thread interrupt rule: the call to a thread's interrupt () method first occurs in the thread's interrupt detection Thread.interrpted ()

Object termination rule: initialization of an object occurs first in the finalize () method

Transitivity: as the name implies, A first occurs in B, B first occurs in C, then A must occur first in C.

This is the end of the content of "what are the rules of the Java 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.

Share To

Development

Wechat

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

12
Report