Specific explanations for Volatilekeyword in Java

Source: Internet
Author: User


First, why should have Volatilekeyword

It is expected that a lot of people who have just started learning about Java have been fooled by the keyword of volatile. Although there are a lot of volatile articles on the web, but they are too much about the underlying principle, but not the application of the scenario, so that the people who have just started to read or confused, and some too use analogy explanation. caused a certain error. This kind of article is more harmful. Below, the younger brother tries to analyze the function and the use method of the Volatilekeyword. Hope to bring a certain revelation to everyone. The error in the text, please the great god correct you.

We know that in multithreaded programming, when multiple threads have access to shared variables, they have to make the necessary synchronizations, otherwise they are very likely to produce an error. The Synchronizedkeyword can be used to provide such synchronization. The keyword primarily sets the method or block of code to synchronous or synchronized code blocks.

Such synchronization can provide, for example, the following two guarantees:

1. Atomic nature. No matter how many threads this method or block of code are interviewed by. The statements in the method and code block can only be run as a whole, that is, when a thread visits a synchronous method, only when the method is finished, the other thread is able to run the method. Simply put, it is a thread, and all of the statements in the synchronization method or code block must be run out.

2, memory visibility.

This refers to any changes that a thread makes to a shared variable in a synchronous method or block of code that is visible to subsequent threads.

Such synchronization mechanism is like a set of thorough suits, very standard, but it is a little awkward to wear, the cost is big. Sometimes it is not very convenient to use, for example, there is a state variable in the program. Read by multiple threads, we only require that each thread read the values to be up-to-date and take different actions based on the latest state. In this case, Java provides us with a lightweight solution, which is to use volatile.

Second, what is the role of Volatilekeyword

The English meaning of volatile is "variable." volatile (of liquids or oils); explosive; merry. Brisk. " It can be seen that, as a single literal understanding, volatile should be used on variable, unstable variables. In fact, indeed, this keyword function is to tell the compiler that any variable declared by the keyword is volatile and unstable.

So instead of trying to use an optimization mechanism like caching for that variable, you should read the value from its memory address every time.

Variables that use volatile tags do not need to use locks when reading or writing, which reduces the probability of deadlocks and keeps the code concise.

Please note that. This is simply to say that every time a volatile variable is read, it is read from its memory address. It does not say that every time a volatile variable is changed, the value is immediately written back to memory. This means that volatile provides only memory visibility. Instead of providing atomicity.

Third, when to use Volatilekeyword

Knowing the role of volatile, we also know where it should be used.

Very clearly. It's best to have a variable that just has a thread churn. Where multiple threads read the variable.

This is the requirement for high memory visibility and low atomicity.

For example, when we write a game. It is often necessary to define a game's state variable and change it in the main thread according to different conditions. To improve game execution efficiency, we typically use multiple threads to process program data concurrently. These threads are asked to access the game's state variables, and the ability to perform the corresponding operation according to the game state. This time it's just right to use the volatile.


Specific explanations for Volatilekeyword in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.