Skip to main content

Posts

Showing posts from September, 2013

What is the diffrence between "volatile" and "synchronized" keywords?

Volatile keyword is thought of as an easy form of synchronized. It require less coding than synchronized and frequently have less run-time overhead. When we talk about locks, 2 main features introduce: i.) mutual exclusion which means only one thread at a time can hold a given lock, and ii.) visibility which ensures that changes mode to shared data before releasing the lock are accessible to another thread that subsequently acquires the lock. Comparing the volatile keyword with synchronized, the volatile is a field modifier while synchronized is used with code blocks and methods. Let's clear one more point here. Threads can have local copies of variables/data which don't have to be the same as the data held in other threads. In fact, java has an idea of a MAIN memory which holds the current value for variables. And threads can have their own copy of variables which can be different from the main memory. And it's possible that suppose you have a variable named X which value