Wednesday 10 February 2016

Important point related to volatile keyword in Java

Important point related to volatile keyword in Java

Volatile keyword can only be applied to variable, it can not be applied to class or method. using volatile keyword along with class and method is compiler error.

2) volatile is also refereed as modifier in Java.

When to use volatile keyword?
1) Any variable which is shared between multiple threads should be made variable, in order to ensure that all thread must see latest value of volatile variable.

2) A signal to compiler and JIT to ensure that compiler does not change ordering or volatile variable and moves them out of synchronized context.

3) You want to save cost of synchronization as volatile variables are less expensive than synchronization.

That's all on What is volatile variable in Java, When to use volatile variable in Java. volatile variable is an important concept to understand and use. Its also very important in terms of  Java interview point of view.

No comments:

Post a Comment