S12L11 – Usage of Volatile keyword

Understanding the Volatile Keyword in Java

Table of Contents

  1. Introduction
  2. The Concept of Volatile in Java
  3. Importance and Benefits of Using Volatile
  4. Example: Volatile in a Multithreaded Application
  5. Code Walkthrough
  6. Conclusion

Introduction

In modern Java applications, multithreading has become a standard approach for optimizing performance. However, with the power of concurrent execution comes the challenge of managing shared data across threads. The volatile keyword is one such mechanism provided by Java to handle shared data between threads in a safe and efficient way.

In this article, we will explore the usage of the volatile keyword, its benefits, and how it can be applied in multithreaded applications. Using a concrete example, we will walk through a codebase that demonstrates volatile in action, ensuring you understand both its syntax and application.

The Concept of Volatile in Java

The volatile keyword in Java is used to indicate that a variable’s value will be modified by different threads. This means that changes made to a volatile variable by one thread are immediately visible to all other threads. This keyword ensures that the variable’s value is read directly from main memory, not from a thread’s local cache, preventing visibility issues in a multithreaded environment.

When to Use Volatile?

The volatile keyword is used in the following scenarios:

  • When multiple threads are modifying a shared variable.
  • When you want to avoid synchronization blocks for lightweight thread coordination.
  • When you do not need atomicity but need to ensure visibility across threads.

Example of Volatile Usage

Consider the following code that demonstrates the usage of volatile in a multithreaded Java application:

Code Walkthrough

Let’s break down the code step by step:

  • volatile public static int flag = 0;: The volatile keyword is applied to the flag variable, indicating that changes to this variable should be visible to all threads.
  • Thread 1: This thread runs an infinite loop where it continuously prints an incrementing integer (i) as long as the flag remains 0. The loop checks the flag value frequently.
  • Thread 2: This thread sleeps for one second and then changes the flag variable to 1, causing Thread 1 to stop executing.
  • Thread Communication: Thanks to the volatile keyword, the updated value of flag in Thread 2 is instantly recognized by Thread 1, demonstrating proper synchronization between threads without explicit synchronization blocks.

Output:

Conclusion

The volatile keyword is a simple yet effective mechanism for managing shared variables in a multithreaded environment. While it does not provide atomicity or complex synchronization, it ensures visibility of changes made to shared variables across threads. This can be especially useful for lightweight thread communication where atomic operations are not required.

In summary, the key takeaways are:

  • volatile ensures visibility across threads.
  • It is used for shared variables that do not require atomic operations.
  • In the provided example, volatile guarantees that Thread 1 correctly detects the update to flag made by Thread 2.