Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

volatile when to use

  Asked By: Phil    Date: May 30    Category: Java    Views: 552
  

i want to know where or when or under what condition volatile keyword will
be used? i found some examples using volatile in the implementation of
threads.
plz some one tell me do we use volatile instances. if yes how it will
improve the performance. give me some real world exmple where we use it?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Nicolas Costa     Answered 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.

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




Tagged: