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 use the volati keyword in Java

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

Share

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

Most people do not understand the knowledge points of this article "how to use the volati keyword in Java", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the volati keyword in Java" article.

First, what is the volatile keyword and what is its function

Volatile is a lightweight synchronization mechanism provided by java virtual machines.

The function is: 1. Ensure visibility 2. Prohibit instruction rearrangement 3. Atomicity is not guaranteed

Second, what is JMM?

JMM (java memory Model Java Memory Model referred to as JMM) is an abstract concept, which does not really exist in memory. It describes a set of specifications or rules through which the access methods of various variables (instance fields, static fields and elements that make up array objects) in the program are defined.

Synchronization rules of JMM:

1. Before the thread is unlocked, the shared variable must be flushed back to main memory

two。 Before a thread can add a lock, it must read the latest value of main memory into its own workspace

3. Lock and unlock must be the same lock

Because the entity that JMM runs the program is a thread. When each thread is created, JMM creates its own working memory (stack space), which is the private data area of each thread. The java memory model stipulates that all variables are stored in the main memory, and the main memory is a shared memory area, which can be accessed by all threads, but the thread's variable operation (read assignment, etc.) must be carried out in its own working memory. First, copy the variable from the main memory to its own working memory, then operate on the variable, and then write the new value of the variable back to the main memory after the operation is completed. The variables of the main memory cannot be manipulated directly. Copies of the variables of the main memory are stored in the working memory of each thread. Because different threads of IC cannot access each other's working memory, the communication between threads must be completed in the main memory.

Third, visibility

Visibility: when multiple threads access the same variable, one thread modifies the value of the variable, and other threads can see the modified value immediately.

Through the previous introduction to JMM, we know that each thread operates on the variables of the main memory by copying them into its own working memory and then writing back to the main memory.

There may be a thread a that modifies the value of the shared variable X but has not yet written back to the main memory, and another thread b operates on the shared variable X. but at this time, the shared variable X of thread a's working memory is invisible to the thread bar, and the problem of synchronization delay between working memory and main memory causes visibility problems.

Fourth, atomicity is not guaranteed.

Atomicity: when a thread is performing a business, it cannot be plugged or divided in the middle, but needs to be complete. Either succeed or fail at the same time

Class MyData {volatile int number = 0; Object object = new Object (); public void addTo60 () {this.number = 60;} public void addPlusPlus () {this.number++;} AtomicInteger atomicInteger = new AtomicInteger (); public void addAtomic () {atomicInteger.getAndIncrement ();}} / * verify the visibility of volatile * 1. When number is not modified by volatile, new Thread changes the number value to 60, but the main thread does not know it and will never get out of the loop * 2. When number is decorated with volatile, and new Thread changes the value of the number, it notifies the mainthread that the value of the main memory has been modified, ending the task. Reflects the visibility * * verification volatile does not guarantee atomicity * 1. Atomicity means that when a thread is performing a business, it cannot be plugged or divided in the middle, but needs to be complete as a whole. Either succeed at the same time or fail at the same time * * how to solve it? * 1. Use synchronize * 2. Use AtomicInteger * * / public class VolatileDemo {public static void main (String [] args) {/ / seeByVolatile (); atomic ();} / / verify atomicity public static void atomic () {MyData myData = new MyData (); for (int I = 1; I

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