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 learn the Java volatile keyword

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

Share

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

Today, I will talk to you about how to learn the Java volatile keyword, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

It is believed that most Java programmers have learned the use of the keyword volatile. The definition of volatile on Baidu encyclopedia:

Volatile is a type modifier (type specifier) designed to modify variables accessed and modified by different threads. The role of volatile is to act as an instruction keyword to ensure that this instruction is not omitted due to compiler optimization and requires a direct reading each time.

There may be many friends who are new to Java who still feel foggy after reading the very general description above.

Let's use a concrete example to learn the use of volatile.

Look at this example:

Public class ThreadVerify {public static Boolean stop = false; public static void main (String args []) throws InterruptedException {Thread testThread = new Thread () {@ Override public void run () {int I = 1; while (! stop) {/ / System.out.println ("in thread:" + Thread.currentThread () + "I:" + I) System.out.println ("Thread stop I =" + I);}}; testThread.start (); Thread.sleep (1000); stop = true; System.out.println ("now, in main thread stop is:" + stop); testThread.join ();}}

This code defines a Boolean variable stop on the second line of the main thread, and then the main thread starts a new thread, constantly increasing the value of counter I in the thread until the main thread's Boolean variable stop is set to true.

The main thread pauses with Thread.sleep for 1 second and sets the Boolean stop to true.

Therefore, the expected result is that the execution of the above Java code stops after 1 second, and the actual value of the counter I within 1 second is printed.

However, after executing the Java application, you find that it enters an endless loop and the CPU occupancy rate of the Java program soars in the task manager.

What is the reason? Let's review the knowledge of the memory model mentioned in the computer professional course operating system.

Taking Java memory model as an example, Java memory model is divided into main memory (main memory) and working memory (work memory). The variables in the main memory are shared by all threads, each thread has its own working memory, and the variables contain thread local variables. If the variable in the main memory is used by the thread, the thread's working memory maintains a copy of the main memory variable.

All read and write operations on variables by a thread must be performed in working memory and cannot directly manipulate variables in main memory. There is also no direct access to each other's working memory between different threads. The transfer of variables between threads needs to be done through the main memory. The interaction among thread, main memory and working memory is shown in the following figure:

If a thread modifies a variable defined in the main thread (main memory) in its own execution code, the modification occurs directly in the thread's working memory, and then at some point (the Java programmer cannot control this moment, but is scheduled by JVM), the modification is written back to the main memory from the working memory.

Let's go back to our example. Although the main thread modifies the stop variable, it only changes the value in the main memory, while the stop variable in the working memory of the thread operating on the counter is still the old value, always false. So the thread is stuck in a dead loop.

Knowing the principle, the solution is simple. Prefix the stop variable with the keyword volatile so that each time the value of stop is read in the counter thread, volatile forces the thread to read from main memory rather than from the working memory of the current thread. This avoids the endless cycle. The following figure shows that after 1 second, the counter executes 1.4 billion times.

After reading the above, do you have any further understanding of how to learn the Java volatile keyword? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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