What is the volatile modifier for Java variables?

Overview of volatile modifiers

--It's a modifier given to variables. In some cases it can be used instead of synchronized. --The following is a usage example.

private static volatile int count = 0; 

Role 1: Prohibit reading from cache value

--If multithreading is used, each thread may cache the value of the field. Role to exclude from this cache (Since each thread prepares a copy of the variable for performance improvement, if the value in the main memory changes, it may deviate from the value in the thread) --If you add volatile, you can request ** read this variable from main memory **.

Role 2: Exclude compile-time optimization

--When Java is compiled, optimization is performed to eliminate unnecessary evaluation.

#The code below...
boolean flg = true;
while(flg){
    System.out.println("hello!");
}

#Optimized as follows...→ The loop does not end on the assumption that flg is changed to false in another thread!
if(flg){
  while(true){
       System.out.println("hello!");
  }
}

--By adding volatile, you can request that you do not perform the above ** optimization **.

How is it different from synchronized?

--Synchronized is more sophisticated (volatile is a simplified version of synchronized) --volatile has the above role, but it is not thread-safe. --volatile has the role of synchronizing variables between the thread and main memory (= reading the value from main memory without caching), but synchronized also has the role of performing exclusive control. -It is wrong to say that ** volatile ** can be used for exclusive control.

References

[Difference Between Volatile and Synchronized Keywords in Java] https://dzone.com/articles/difference-between-volatile-and-synchronized-keywo

Recommended Posts

What is the volatile modifier for Java variables?
What is the constructor for?
What is java
What is Java <>?
What is Java
What is the best file reading (Java)
What is the main method in Java?
What is the Facade pattern useful for?
What is the Java Servlet / JSP MVC model?
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What Java engineers need to prepare for the Java 11 release
What is the difference between Java EE and Jakarta EE?
What is Java Assertion? Summary.
What is the pluck method?
What is the BufferedReader class?
What is a Java collection?
[Ruby] What is `!!` used for?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
Java is the 5th day
What is the initialize method?
What is the LocalDateTime class? [Java beginner] -Date and time class-
What is JVM (Java Virtual Machine)?
'% 02d' What is the percentage of% 2?
Where is the Java LocalDateTime.now () timezone?
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is a lambda expression (Java)
The procedure for applying for the Java Bronze certification exam is too complicated.
Kantai Collection Java # 0 What is Object Oriented Programming (OOP)? [For beginners]
[Java] What is the difference between form, entity and dto? [Bean]
What is a class in Java language (3 /?)
What is the right migration data type? ??
Introduction to java for the first time # 2
What is testing? ・ About the importance of testing
[For programming beginners] What is a method?
About the procedure for java to work
Java Programming Style Guide for the Java 11 Era
What is a class in Java language (1 /?)
[For super super beginners] What is object orientation?
What is Java and Development Environment (MAC)
[Java] Nowadays, the extended for statement is not exclusively for List, isn't it?
What is the model test code testing
What is Docker? What purpose is it used for?
What are the updated features of java 13
What is the data structure of ActionText?
The Java EE Security API is here!
Learning for the first time java [Introduction]
[Java] Variables declared inside the `for statement` cannot be used outside the` for statement block`
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
Java vs. JavaScript: What ’s the Difference?
What is ... (3 dots) found in the Java source? Variadic arguments to know from
What is Pullback doing in The Composable Architecture
What is JSP? ~ Let's know the basics of JSP !! ~