I understand volatile
should be used on a class field to prevent the JVM from caching the value so when it will always be the latest value when it's read.
If my understanding is correct, doesn't it mean we should define all fields with volatile
when working in a multithread thread-safe environment? When is it unnecessary to define a field as volatile
?