If you know that the object will not change (for example, immutable objects, such as String or Integer), and therefore avoid using any of the synchronization constructs ( synchronized, volatile), counting this object from several threads does not affect performance. All threads will access memory where the object is stored in parallel.
The JVM may choose, however, to cache some values locally in each thread for performance reasons. Using volatileprohibits exactly this behavior - the JVM will have to explicitly and atomically access the field volatileevery time.
source
share