What is the Java volatile keyword?
Daily we are reading about the java its key points, today we are going to get the internals of volatile keyword, volatile is used to indicate that a variable is going to reside in volatile memory only i.e. Main memory aka RAM as we know that when CPU operates on any variable it will copied that object into its registers and it will operate on the register and after completion of its operation it will send the modified value to RAM, so in case of a multi-threaded application it may possible that two thread will operate on same variable but under different CPU registers so two avoid this kind of situation we can use "volatile" keyword with the variable.
Declaring a volatile Java variable means:
The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory" aka RAM.
Now we can say that The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache/registers, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.
volatile is Not Always Enough
Even if the volatile keyword guarantees that all reads of a volatile variable are read directly from main memory, and all writes to a volatile variable are written directly to main memory, there are still situations where it is not enough to declare a variable volatile.
Performance Considerations of volatile
Reading and writing of volatile variables causes the variable to be read or written to main memory and we all know that main memory is too slow in the comparison of CPU registers. So reading from and writing to main memory is more expensive than accessing the CPU cache/registers. Thus, you should only use volatile variables when you really need to enforce visibility of variables.
Daily we are reading about the java its key points, today we are going to get the internals of volatile keyword, volatile is used to indicate that a variable is going to reside in volatile memory only i.e. Main memory aka RAM as we know that when CPU operates on any variable it will copied that object into its registers and it will operate on the register and after completion of its operation it will send the modified value to RAM, so in case of a multi-threaded application it may possible that two thread will operate on same variable but under different CPU registers so two avoid this kind of situation we can use "volatile" keyword with the variable.
Declaring a volatile Java variable means:
The value of this variable will never be cached thread-locally: all reads and writes will go straight to "main memory" aka RAM.
Now we can say that The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache/registers, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.
volatile is Not Always Enough
Even if the volatile keyword guarantees that all reads of a volatile variable are read directly from main memory, and all writes to a volatile variable are written directly to main memory, there are still situations where it is not enough to declare a variable volatile.
Performance Considerations of volatile
Reading and writing of volatile variables causes the variable to be read or written to main memory and we all know that main memory is too slow in the comparison of CPU registers. So reading from and writing to main memory is more expensive than accessing the CPU cache/registers. Thus, you should only use volatile variables when you really need to enforce visibility of variables.
Comments
Post a Comment