• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Volatile Modifiers

 
Ranch Hand
Posts: 198
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read a document which say Volatile Modifiers, are not cached by the JVM. When ever a volatile variable value is required by a thread it will access directly from the variable itself (without using the JVM cache)..

Volatile: Volatile modifier tells the compiler that the variable modified by
volatile can be changed unexpectedly by other parts of the program. For
example a Variable might be read from Cache and not update the content if it
has been changed by another thread. Specifying a variable as volatile tells
the JVM that any threads using that variable are not allowed to cache that
value at all. Making the Variable Volatile will ensure that the compiler
will get the content of the variable every time it is used and not cache its
content. If not used Carefully this modifier might introduce bugs..




is my understanding is right ???... can you please share me in which real time scenarios we will use volatile modifiers....
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only use it in a multi-threaded environment. A simple example:
Usually, run() is called in one thread (the one you start using the Runnable), and stop() is called from another thread (like the event thread in AWT / Swing or the main thread). If in line 1 the cached value of active is used, the loop may not stop when you set active to false in line 2. That's when you want to use volatile.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any variable can live in RAM, in Cache or in the registers on the chip. When a method finishes, the value may be stored in any of those plaecs in case it is needed again; if not it is moved back from register to cache to RAM. If two threads require access to a variable, they might pick it up from RAM, but there might be a more "recent" or "up-to-date" version in cache or a register. So(if I remember correctly) "volatile" means that methods must only use the "RAM" version of a variable, and when they have finished they must write their result back in RAM.

So the RAM version of a "volatile" variable is always the most "recent."
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that it is unfortunate that the document used "cache" -- as people assumes that it is the hardware cache. I don't think it does.

The memory subsystems, along with the L1/L2 caches, should be able to keep everything in sync, and provide the latest versions. IMHO, when the document says cache, I think it means that it will not delay writes of register to memory, or assumes that the register has the latest version.

IOWs, don't use registers to cache the value -- I don't think it was referring to the hardware caches.

Henry
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
people assumes that it is the hardware cache.



Especially the idea of "the cache" is really dangerous. Modern machines have so many cache levels and in the server world, separate multi-core processors, that manual thinking about cache is bad.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic