We will cover the following about Volatile keyword in this section:
1) What is Volatile Keyword.
2) When to use it?
3) Some points to note about Volatile .
4) Why it is always compared with Synchronized ?
5) Comparison of Volatile and Synchronized :
Let's start the journey:
1) What is Volatile Keyword.
Although, it is rarely used keyword in java yet is the important concept to understand. We can understand this keyword in straight words by saying that the variables which are declared as volatile , the value of those variable will be stored into the main memory directly every time they are updated by the different threads. Hence, every thread will get an updated value of these variables. The volatile keyword can only be applied to a variable, it can not be applied to a class or a method. using volatile keyword along with class and method will result in a compiler error.
2) When to use it?
This variable should be used with the Multi-threading(for example). If we don't use volatile keyword then the Thread X will not know that variable has been changed by Thread Y. Let's take an example:
public class ABC extends Thread {
private volatile boolean takeAPause;
public void run() {
while (!takeAPause) {
//further code
}
}
public void commandForStop() {
takeAPause = true;
}
}
Here, If the variable is not declared as volatile, Then the current thread which is in loop will cache the value of the variable at the first round and will never read it again from the memory . By keeping this as volatile, it is guaranteed that the value will come directly from the memory in each round of the loop.
3) Some points to note about Volatile .
* The volatile in Java is only applicable to a variable and using volatile keyword with class and method is a compiler error.
* Volatile guarantees that value of the volatile variables will always be read from main memory directly and not from Thread's local cache.
* Reads and writes are atomic for all variables declared using volatile keyword.
* Changes to a volatile variable are always visible to other threads.
* An access to a volatile variable in never has a chance to block any functionality as this is
4) Why it is always compared with Synchronized?
It is always compared to the Synchronized because both the concepts are under the hood of multi-threading and are related to the variable access-ness throughout the multiple threads.
5) Comparison of Volatile and Synchronized :
Lock : Volatile takes the lock on the variable level globally while Synchronized takes the lock on the monitor level globally.
Null : Null is not allowed in Synchronized while null is allowed in Volatile.
Blocking : Blockage can occur due to Synchronized while blockage can not occur due to Volatile.
Performance : We should use volatile if we are really concerned about the performance other than Synchronized.
1) What is Volatile Keyword.
2) When to use it?
3) Some points to note about Volatile .
4) Why it is always compared with Synchronized ?
5) Comparison of Volatile and Synchronized :
Let's start the journey:
1) What is Volatile Keyword.
Although, it is rarely used keyword in java yet is the important concept to understand. We can understand this keyword in straight words by saying that the variables which are declared as volatile , the value of those variable will be stored into the main memory directly every time they are updated by the different threads. Hence, every thread will get an updated value of these variables. The volatile keyword can only be applied to a variable, it can not be applied to a class or a method. using volatile keyword along with class and method will result in a compiler error.
2) When to use it?
This variable should be used with the Multi-threading(for example). If we don't use volatile keyword then the Thread X will not know that variable has been changed by Thread Y. Let's take an example:
public class ABC extends Thread {
private volatile boolean takeAPause;
public void run() {
while (!takeAPause) {
//further code
}
}
public void commandForStop() {
takeAPause = true;
}
}
Here, If the variable is not declared as volatile, Then the current thread which is in loop will cache the value of the variable at the first round and will never read it again from the memory . By keeping this as volatile, it is guaranteed that the value will come directly from the memory in each round of the loop.
3) Some points to note about Volatile .
* The volatile in Java is only applicable to a variable and using volatile keyword with class and method is a compiler error.
* Volatile guarantees that value of the volatile variables will always be read from main memory directly and not from Thread's local cache.
* Reads and writes are atomic for all variables declared using volatile keyword.
* Changes to a volatile variable are always visible to other threads.
* An access to a volatile variable in never has a chance to block any functionality as this is
4) Why it is always compared with Synchronized?
It is always compared to the Synchronized because both the concepts are under the hood of multi-threading and are related to the variable access-ness throughout the multiple threads.
5) Comparison of Volatile and Synchronized :
Lock : Volatile takes the lock on the variable level globally while Synchronized takes the lock on the monitor level globally.
Null : Null is not allowed in Synchronized while null is allowed in Volatile.
Blocking : Blockage can occur due to Synchronized while blockage can not occur due to Volatile.
Performance : We should use volatile if we are really concerned about the performance other than Synchronized.
No comments:
Post a Comment