Volatile variable have some specific uses.It's better to know all the major reason why java adopt such variables .Please found the below 10 points which need to be noticed:
1.Volatile variable always get allocated in the main memory.To allocate any variable in main memory volatile keyword is used.
2.Precisely volatile variable are always read and write directly from main memory.It doesn't perform any operation from CPU 's cache.
3.Volatile variable after JAVA 5 guranatees the visibilty of changes to variable across multiple threads.
4.Volatile also provide atomicity in some cases e.g .while reading double and long data types.
5.In case of volatile long and volatile double data types which are of 64 bytes the read operation is atomic.
6.If you know that a long field is accessed by more than one thread e.g. a counter or anything, you better make it volatile. Why? because reading to a long variable is not atomic in Java and done in two steps, If one thread is writing or updating long value, it's possible for another thread to see half value (fist 32-bit). While reading/writing a volatile long or double (64 bit) is atomic.
7.The above point also shows that the volatile keyword can also perform non-atomic variable to perform atomic operation in some cases(e.g. long and double).
8.Reading or writing a volatile variable does not block threads reading or writing. For this to happen you must use the synchronized keyword around critical sections.
9.One important usecase you need to know that while you are defining a volatile array .It doesn't provide volatile access to every member of array.
for example:
volatile int[] arr=new int[100];
here only the instance is volatile now if at any time you change
arr[x]=y from a thread and now if you read from another thread then it will never be happened that arr[x] equals to y.
10.In above usecase if you want to give volatile accessibilty to each member of array you need to use the atomic data types.Also you can use same referencing to the member of array to guarantee the visibilty of changes.
1.Volatile variable always get allocated in the main memory.To allocate any variable in main memory volatile keyword is used.
2.Precisely volatile variable are always read and write directly from main memory.It doesn't perform any operation from CPU 's cache.
3.Volatile variable after JAVA 5 guranatees the visibilty of changes to variable across multiple threads.
4.Volatile also provide atomicity in some cases e.g .while reading double and long data types.
5.In case of volatile long and volatile double data types which are of 64 bytes the read operation is atomic.
6.If you know that a long field is accessed by more than one thread e.g. a counter or anything, you better make it volatile. Why? because reading to a long variable is not atomic in Java and done in two steps, If one thread is writing or updating long value, it's possible for another thread to see half value (fist 32-bit). While reading/writing a volatile long or double (64 bit) is atomic.
7.The above point also shows that the volatile keyword can also perform non-atomic variable to perform atomic operation in some cases(e.g. long and double).
8.Reading or writing a volatile variable does not block threads reading or writing. For this to happen you must use the synchronized keyword around critical sections.
9.One important usecase you need to know that while you are defining a volatile array .It doesn't provide volatile access to every member of array.
for example:
volatile int[] arr=new int[100];
here only the instance is volatile now if at any time you change
arr[x]=y from a thread and now if you read from another thread then it will never be happened that arr[x] equals to y.
10.In above usecase if you want to give volatile accessibilty to each member of array you need to use the atomic data types.Also you can use same referencing to the member of array to guarantee the visibilty of changes.
Comments
Post a Comment