I've been told that the use of a volatile variable is much more efficient than using a synchronized block whenever using it (read or write).
Up until Java 1.4 I can see why (because then threads didn't have to flush and refresh all accessible memory).
But since Java 1.5, the only difference I see between using volatile variable and a synchronized block - is the lock acquiring mechanism.
Is acquiring the lock really that expensive? And if so, why is that?
The "expense" is because of it reading from memory every time, instead of possibly using the memory caches. It is not a lot, at least on x86 machines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With