Java Operators

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - volatile keyword



volatile keyword is used to mark a variable as volatile so that in can be easily modified in multi-threaded environment. Volatile keywords are thread safe. Multiple threads can access a volatile variable in any method easily without corrupting its value.

A volatile variable does not cache the value in cpu cache. It always stores the value in main memory and retrieves from the same main memory. For example, in multiple CPU environment, multiple threads can work on different CPU but volatile variable will always remains in main memory instead of CPU cache temporarily.

Consider following variable −

public class MyRunnable implements Runnable {
   private static int value;
}

If there are multiple threads accessing the instance variable value then each thread will store a local copy of the variable and it may lead to inconsistent value during program execution.

Now consider following variable −

public class MyRunnable implements Runnable {
   private static volatile int value;
}

If there are multiple threads accessing the instance variable value then each thread will use only one copy of the variable stored in main memory and it makes program thread safe.

Important Points to consider

  • volatile keyword can be used for primitives as well as objects.

  • volatile keyword cannot be used on methods or classes.

  • volatile keyword can be used as an alternate for synchronization.

  • volatile variable read and write operations are atomic.

  • If a variable is not shared among threads then do not use volatile keyword.

  • If a variable is not shared among threads then do not use volatile keyword.

Following example shows the use case of volatile keyword.

Example

package com.tutorialspoint;

class ThreadDemo extends Thread {	  	 
   private static  int threadCounter;

   ThreadDemo() {	 		   
   }

   public void run() {	
      threadCounter++;
      System.out.println("Thread counter " +  threadCounter );
   }	   
}

public class TestThread {

   public static void main(String args[]) throws InterruptedException {
      int totalThreads = 5;
      Thread[] threads = new Thread[totalThreads];

      for(int i = 0; i < totalThreads; ++i) {  
         threads[i] = new ThreadDemo();  
      }   
      for(int i = 0; i < totalThreads; ++i)  {
         threads[i].start();                 
      }
      for(int i = 0; i < totalThreads; ++i)  {
         threads[i].join();                
      }  
   }
}

Output

Compile the above program. This will produce the following result −

Thread counter 2
Thread counter 3
Thread counter 2
Thread counter 5
Thread counter 5
java_basic_syntax.htm
Advertisements