• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Volatile Keyword in JAVA

January 25, 2013 By j2eereference Leave a Comment

Use of Volatile Keyword in JAVA

There is a keyword in java named as ‘Volatile ‘. Keywords are basically reserve words which have specific meaning. The value of volatile variable will never be cached thread-locally: all reads and writes will go straight to “main memory”;. It means, there is no cache, volatile is an indicator to java compiler and thread that do not cache value of this variable in which read and write operation directly performs through main memory. The volatile keyword guarantees that all the threads should see the same value of a specified variable.

If you are working with the multithreaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it’s updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn’t know anything about the values changed by another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever threads are updating the values, it is updated to the main memory. So, other threads can access the updated value.

Below is the example which demonstrates this properly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class VolatileExample extends Thread {
 
private volatile int volatileValue;
 
public VolatileExample (String str){
super(str);
}
public void run()
{
int i;
for(i = 0; i< 3; i++)
{
try {
System.out.println(getName() + " : "+i);
if (getName().equals("First ")){
volatileValue= 10;
System.out.println( "Test Value : " + volatileValue);
}
if (getName().equals("Second ")){
System.out.println( "Test Value : " + volatileValue);
}
Thread.sleep(1000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
}
 
public class ExampleMain{
public static void main(String args[]) {
new VolatileExample ("First ").start();
new VolatileExample ("Second ").start();
}
}

Output:

volotile

Each thread has its own stack, and so its own copy of variables it can access. When the thread is created, it copies the value of all accessible variables in its own memory. The volatile keyword is used to say to the jvm “Warning, this variable may be modified in another Thread”. Without this keyword the JVM is free to make some optimizations, like never refreshing those local copies in some threads. The volatile keyword forces the thread to update the original variable for each variable.

Related Posts

  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • What is parallel Stream
  • reduce method of the Stream class
  • Difference between the findFirst() and findAny() method
  • intern() method of String class
  • SOLID – Five principles of object-oriented software design
  • Java Coding Best Practices
  • How to use lambda expression effectively
  • Enhanced pseudo-Random Number Generators in java17
  • How to use Foreign-Memory Access API
  • Pattern Matching for instanceof
  • Text Blocks – Feature added in Java17
  • Record – The new feature added in java 17
  • What is Sealed Class
  • Features added in Java 17
  • Java Buzzwords

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.