java - Does variables accessed within synchronized block must be declared volatile? -


in example this:

...     public void foo() {         ...         synchronized (lock) {             vara += some_value;         }         ...     } ... 

the question is, vara must declared volatile in order prevent per-thread caching or enough access within synchronized blocks?

thanks!

no, don't need to.
synchronized blocks imply memory barrier.

from jsr-133:

but there more synchronization mutual exclusion. synchronization ensures memory writes thread before or during synchronized block made visible in predictable manner other threads synchronize on same monitor. after exit synchronized block, release monitor, has effect of flushing cache main memory, writes made thread can visible other threads. before can enter synchronized block, acquire monitor, has effect of invalidating local processor cache variables reloaded main memory. able see of writes made visible previous release.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -