Amazon Interview Question
Software Engineer / DevelopersCountry: United States
It doesn't mean, use only static volatile (or) volatile..the usage should depend on your requirement.
volatile is only used for visibility among all the threads..if one thread changes the field value, the changed value is reflected to all the threads only if the field is declared volatile.
static, meaning the field is associated with the class, but not the object.
If the field is made static volatile it means that the changes in the "class field" is made visible to all the threads acting on that class (or) all the object instances.
If its not static but only volatile, which means the changes in the "instance field" is made visible to all threads acting on that particular object instance.
Ref: stackoverflow.com/questions/2423622/volatile-vs-static-in-java
- Anonymous March 17, 2013Even if you access a static values through multiple thread each thread can have it's local cached copy! To avoid this you can declare the variable as static volatile and this will force the thread to read each time the global value