Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Phil Sanchez   on May 30 In Java Category.

  
Question Answered By: Nicolas Costa   on May 30

Volatile variables are ones that can change
asynchrously with the thread, such as
variables that are shared with another
thread or ones that represent hardware
registers that may change because of some
outside condition. An example would be
the result of an analog-to-digital
conversion. Normally the compiler can
assume that when it sets a variable it will
stay set, and not necessarily retrieve it
the next time it is needed because it
assumes the value hasn't changed. In order
to optimize, it may keep track of a copy
in a processor register and use that instead.
The volatile keyword  tells the compiler not
to do that, and to fetch it again each time
it is referenced.

Using volatile variables is beyond my
programming experience in Java, but I've
used them in embedded C applications that
need to respond to changing hardware
conditions. I don't think they would come
up that often in the typical Java app.

Share: 

 
 
Didn't find what you were looking for? Find more on volatile when to use Or get search suggestion and latest updates.


Tagged: