Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Volatile Keyword in Java

Last Updated on April 28, 2023 by Prepbytes

Multithreading is a powerful feature in Java that allows for the concurrent execution of multiple processes, which can optimize computation and enhance system responsiveness. However, despite its advantages, multithreading can create loopholes that developers must address to ensure stable software. One such loophole is the "Visibility Problem," which can be overcome using the volatile keyword in Java. In this article, we’ll learn about volatile keywords in Java and we will also explore various benefits of using volatile keyword in Java. But before we jump in, let’s first understand the Visibility Problem and why it’s such a critical issue to resolve.

Visibility Problem in Multithreading

In a multi-threaded environment, if variables are defined as non-volatile, each thread in the CPU will copy the variables from the main memory to the CPU Caches as illustrated in the image given below.

Both threads will update the value of the variables as per their needs. Now, the value set by Thread1 is not updated back to the main memory, so Thread2 cannot see the latest value of the variable. This may lead to inconsistent data.

This problem of not being able to see the latest value of the variables by the threads, since it is not updated in the main memory is known as the “Visibility Problem”.

To solve the visibility problem, we use the volatile Keyword in Java.

What is the Volatile Keyword in Java?

The volatile keyword in Java is used to indicate that a variable’s value may be modified by different threads concurrently. When a variable is declared as volatile, it ensures that any change made to it by one thread is immediately visible to all other threads, preventing the possibility of inconsistent data and unexpected behavior. The volatile keyword also guarantees that operations on the variable are executed in the correct order, and no thread can reorder the instructions for performance optimization. Therefore, using the volatile keyword in Java is an effective way to ensure thread safety and avoid concurrency issues.

Here is an example of using the volatile keyword in Java:

Example Code for volatile Keyword in Java:

public class MyRunnable implements Runnable {
    private volatile boolean flag = false;

    @Override
    public void run() {
        while (!flag) {
            // do some work here
            // Statements to exceute
        }
        System.out.println("Thread is finished.");
    }

    public void stop() {
        flag = true;
    }
}

Explanation:
Here in the above code, the flag variable is marked as volatile by using the volatile keyword in Java. The MyRunnable class is a runnable task that loops until the flag variable is set to true. The stop() method can be used to set the flag variable to true, causing the thread to exit the loop and finish. Here we have defined the flag as volatile so that two different threads using the variable will always have the updated value of the variable and thus will solve the visibility problem.

When to Use the Volatile Keyword in Java?

The volatile keyword in Java should be used when a variable is accessed and modified by multiple threads concurrently, and its value needs to be visible to all threads in real-time. Here are some scenarios where using the volatile keyword is appropriate:

  • Status flags: If you have a flag that indicates the status of a process, such as a task that is currently running, you may want to mark it as volatile. This will ensure that all threads can see the updated status of the process.
  • Counters: If you have a counter that is incremented by multiple threads, you may want to mark it as volatile. This will ensure that all threads can see the current value of the counter and avoid race conditions.
  • Configuration variables: If you have a variable that is used to configure the behavior of a system, such as a timeout value or a maximum number of connections, you may want to mark it as volatile. The usage of the volatile Keyword in Java will ensure that all threads can see the current value of the configuration variable and adjust their behavior accordingly.

In general, using the volatile keyword in Java is appropriate when you need to ensure the visibility and consistency of shared data across multiple threads. However, it’s important to note that using volatile does not provide synchronization or mutual exclusion mechanisms. Therefore, it should not be used to replace locks or other synchronization primitives when they are required.

Behavior of Volatile Keyword in Java

Here are some important points to keep in mind about the behavior of the volatile keyword in Java:

  • Visibility: Usage of the volatile keyword in Java ensures that the all write operations on a shared variable are visible to each and every threads in the CPU.
  • Atomicity: Atomicity is the property that only a single thread is modifying the variable at a single time. The volatile Keyword in Java does not provide atomicity guarantees. For achieving atomicity, you have to use synchronization.

To learn more about this, refer to this article – Synchronization in Java.

Examples of using Volatile Keyword in Java

Let’s look at some examples of using the volatile keyword in Java.

Example 1 of Volatile Keyword in Java: Status Flag
The following example demonstrates how the status flags in Java are defined as volatile.

Code Implementation:

public class MyRunnable implements Runnable {
    private volatile boolean running = false;

    @Override
    public void run() {
        running = true;
        while (running) {
            // do some work here
        }
        System.out.println("Thread is finished.");
    }

    public void stop() {
        running = false;
    }
}

Explanation:
In this example, the running variable is marked as volatile. The MyRunnable class is a runnable task that loops until the running flag is set to false. The stop() method can be used to set the running flag to false, causing the thread to exit the loop and finish and thus reducing the chances of data inconsistency while working in the multi-threaded environment.

Example 2 of Volatile Keyword in Java: Counter
The counter variable which is being used in different threads for the purpose of counting should be declared as volatile to eliminate data inconsistency in the value of a counter.

Code Implementation:

public class Counter {
    private volatile int count = 0;

    public void increment() {
        count++;
    }

    public int getCount() {
        return count;
    }
}

Explanation:
In this example, the count variable is marked as volatile using the volatile Keyword in Java. The Counter class is a simple counter that can be incremented by multiple threads. The increment() method increments the count variable, and the getCount() method returns the current value of the count variable.

Example 3 of Volatile Keyword in Java: Configuration Variable
The given example shows how the configuration variable is defined as volatile in Java.

Code Implementation:

public class ConnectionPool {
    private volatile int maxConnections = 10;

    public void setMaxConnections(int maxConnections) {
        this.maxConnections = maxConnections;
    }

    public int getMaxConnections() {
        return maxConnections;
    }
}

Explanation:
In this example, the maxConnections variable is marked as volatile. The ConnectionPool class is a connection pool that can be configured with a maximum number of connections. The setMaxConnections() method sets the maxConnections variable, and the getMaxConnections() method returns the current value of the maxConnections variable.

Benefits of using Volatile Keyword in Java

The benefits of using the volatile keyword in Java are:

  1. Ensuring visibility of the latest value of a variable across multiple threads.
  2. Preventing race conditions and ensuring consistency of data.
  3. Avoiding the need for explicit locking mechanisms like synchronized blocks or the use of locks.
  4. Making code simpler and easier to understand by reducing complexity related to synchronization.
  5. Enabling safe and efficient communication between threads.
  6. Enabling more efficient use of CPU caches by avoiding unnecessary memory accesses.
  7. Reducing the risk of deadlocks and livelocks.
  8. Improving scalability and responsiveness of multi-threaded applications.

Conclusion
In conclusion, the volatile keyword in Java is a powerful tool for ensuring the visibility and consistency of shared data in a multi-threaded environment. It allows multiple threads to access and modify the same variable concurrently while ensuring that the updated value is immediately visible to all threads. However, it should be used carefully and appropriately, as it does not provide synchronization or mutual exclusion mechanisms. By understanding the behavior and appropriate use cases of volatile keyword in Java, developers can build more stable and reliable concurrent software.

FAQs on Volatile Keyword in Java

Here are some frequently asked questions on volatile Keyword in Java.

Q1: Does the volatile keyword in Java provide synchronization or mutual exclusion mechanisms?
Answer: No, the volatile keyword in Java does not provide synchronization or mutual exclusion mechanisms. It only ensures the visibility and consistency of shared data.

Q2: Can we use the volatile keyword in Java instead of locks or other synchronization mechanisms?
Answer: No, the volatile keyword in Java should not be used to replace locks or other synchronization mechanisms when they are required.

Q3: Is it possible to use non-primitive data types with volatile keyword in Java?
Answer: Yes, it is possible to use the volatile keyword with non-primitive types such as objects or arrays.

Q4: Can we use final variables with volatile keyword in Java?
Answer: No, the volatile keyword in Java cannot be used with final variables. Final variables are already guaranteed to be visible to all threads.

Q5: Does the volatile keyword in Java guarantee thread safety?
Answer: No, the volatile keyword in Java does not guarantee thread safety. It only ensures the visibility and consistency of shared data.

Q6: What is the difference between synchronized and volatile keyword in Java?
Answer: The volatile keyword in Java ensures the visibility and consistency of shared data, while the synchronized keyword in Java provides both visibility and mutual exclusion mechanisms.

Q7: Can we use static variables with volatile keyword in Java?
Answer: Yes, the volatile keyword in Java can be used with static variables to ensure the visibility and consistency of shared data across all threads.

Leave a Reply

Your email address will not be published. Required fields are marked *