What is the purpose of a Volatile Variable?


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

public class DistributedObject {
    public volatile int count = 0;
0 answers