JAVA : VOLATILE variable
Volatile - easily evaporated from cpu cache
By declaring the counter variable volatile all writes to the counter variable will be written back to main memory immediately.
The volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory
Instead of synchronized variable in Java, you can have java volatile variable, which will instruct JVM threads to read value of volatile variable from main memory and don't cache it locally. If a variable is not shared between multiple threads no need to use volatile keyword with that variable
Volatile - easily evaporated from cpu cache
By declaring the counter variable volatile all writes to the counter variable will be written back to main memory immediately.
The volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory
Instead of synchronized variable in Java, you can have java volatile variable, which will instruct JVM threads to read value of volatile variable from main memory and don't cache it locally. If a variable is not shared between multiple threads no need to use volatile keyword with that variable
public class SharedObject {
public
int counter = 0;
}
public class SharedObject {
public
volatile int counter = 0;
}
No comments:
Post a Comment