Volatile variable values can be modified by different threads. They will never have the chance to block and hold a lock. Synchronization will happen whenever the variables are accessed. Using volatile may be faster than a lock, but it will not work in some situations. The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly.
Sample code for volatile variable is shown below:Volatile Variable
12345 | public class Distributed Object {Â Â Â Â public volatile int count = 0;} |