This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Usage of Volatile keyword  RSS feed

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I came across a doc which stated that the usage of "volatile" fields in a class maintains sequential consistency of values.
I have not used volatile much in my coding and so could you give me a brief description about the appropriate usage and uses of using it.
Also please give me hint of how out JIT compilers have implemented the volatile keywords usage.

Thanks in advance,
Shemida
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volatile keyword is important because sometimes JVM caches the value of a variable for performance. In a multi-threaded system this can be a problem as a value set by one thread might not be visible to other threads for some time. If you don't want the JVM to cache the value of the variable, then you can make it volatile...
 
Bartender
Posts: 9498
184
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The volatile keyword simply tells the program that intermediary results of calculations on the volatile variable should not be cached.

Normally a program might leave a value in the processor register or in the memory cache or something like this, and only write it back to main memory at unknown points in time. This is fine for sequential execution, but if multiple threads try to access such a variable in memory, they might be accessing an old version of the variable, while the most current version is still in a register or in the cache.
Making a variable volatile prevents this. After every operation on it, its value gets written back to main memory. This is slower, but ensures that every thread is looking at the same value when they inspect a variable.

If a variable is edited and read by multiple threads, and access is restricted by synchronized blocks, then you don't have to worry about using volatile. When a thread enters a synchronized block, operations on that variable will be cached as they normally are, and as soon as the thread leaves the synchronized block, the final value in the cache is automatically written back to memory.

So in conclusion, you only have to use volatile if multiple threads access a variable, and the variable isn't protected by a synchronized block.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!