synchronized is much more expensive than plain volatile.
volatile is useful when you just need to read/write single variable and don't care about atomicity of complex structures.
synchronized is useful when you need to perform complex operations, update several variables or set one variable when compared another one and ensure the atomicity of such operation. Also it is used when doing higher level synchronization such as conditions, i.e. synchronized/wait/notify in java. But for that Lock/Condition can be used too.
For even better explanation about using volatile variables you can view the following link with JB Nizet's answer. It compliments well the answer posted by Zbynek and further explains the relation between volatile, atomic variables & complexity. Hope this helps.
Asked in February 2016Viewed 3,803 timesVoted 8Answered 2 times