This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
What is the use of the volatile keyword?
338
Work today, I met volatileJava the keyword. I’m not familiar with it, so I found this explanation.
Given that the article explains the keyword in question in detail, have you ever used it, or have you ever seen a situation where the keyword can be used correctly?
Answer:
Volatile has the semantics of memory visibility. Basically, the value of a volatile field becomes visible to all readers (especially other threads) after the write to it is complete. Without volatile, the reader might see some unupdated values.
To answer your question: Yes, I use a volatile variable to control whether some code continues to loop. Loop through the test for volatile and, if it is, continue true. You can set the condition to false by calling the stop method. False After the stop method completes execution, the loop will see and terminate when the value is tested through the loop.
My highly recommended book “Java Concurrency in Practice” provides a good explanation of volatile. The book was written by the same person who wrote the IBM article cited in the question (in fact, he quotes his book at the bottom of the book). My use of volatile is what his article calls “pattern 1 status flags.”
If you want to learn more about how volatile works behind the scenes, read the Java Memory Model. If you want to go beyond this level, read excellent computer architecture books like Hennessy&Patterson and read about cache consistency and cache consistency.
The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 1…
The authors suggest that this is important: the memory model, even to the MESI protocol, bus storm, static instruction reordering, as-IF-serial (no matter how reordering, the results of single-thread execution cannot be changed). , memory barrier (Memory barrier instructions to disallow reordering of a particular type of handler.) , happens-before (If the results of one operation need to be visible to another, there must be a happens-before relationship between the two operations.)
The volatile modifier applies to the following scenarios:
An attribute is shared by multiple threads. If one thread changes the attribute, the other threads can immediately obtain the modified value, such as BooleanFlag. Or as a trigger for lightweight synchronization.
Volatile, whose reads and writes are lockless, is not a substitute for synchronized because it provides no atomicity or mutual exclusion. It is low-cost because there is no lock and no time to acquire and release the lock. v
Olatile only works on attributes, and we use volatile to modify attributes so compilers don’t reorder those attributes. Volatile provides visibility. Changes made by any thread are immediately visible to other threads. Volatile properties are never cached by threads and are always read from main memory.
Volatile provides a happens-before guarantee that writes to volatile v are happens-before all subsequent reads to V by other threads.
Volatile makes the assignment of long and double atomic. Volatile ensures security by enabling visibility and disallowing instruction reordering in singleton double-checks.
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️