Java: volatile and synchronized

Call it attention deficiency or getting old, I need to remind myself every often what is volatile - hm? or may be because I never used it.
volatile : field modifier. Synchronizes value of a given variable between thread memory and “main memory” – you are guaranteed to get the most recently written value (thanks Josh Bloch) of the variable whenever you refer it.
synchronized : modifier for blocks and methods. Synchronizes value of *all* variables between thread memory and “main memory” ; locks and releases monitor.
While volatile make sure that the thread’s variable has the latest value of main memory, it may supply corrupt data while a change to that variable is in progress. synchronized allows you to lock an execution path until you are done completely modifying the shared resource.

Leave a Comment