• 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

about volatile

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the java 2 exam cram, the keyword volatile is used to signal to the compiler that the designated variable may be changed by multiple Threads and that it can not take any shortcuts when retrieving the value in this variable. What's the meaning of CANNOT TAKE ANY SHORTCUTS? Can anyone give me an example on the use of volatile?
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its mostly used in asynch thread enviroments where variable values are cached due to performance reasons or optimization reasons

Basically, when multiple threads are sharing a variable, the read
and write of the master value and copy value should avoid
inconsistent values.
Volatile helps. It informs the compiler not to optimize that variable, there by when write takes place in the master, the read
operation gives the current value from the master.
HTH
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
Just to expand a bit on Ragu's post: threads act like mini-processes. When one is started it is given it's own memory space and the values of any variables it will need are copied to that memory. The thread executes, modifies it's copies of the variables, and then puts the results back into the main memory variable.
If you know that a particular variable is going to be accessed by multiple threads, as well as non-synchronized code you can mark the variable as 'volatile'. When the compiler sees that a thread will access a 'volatile' variable, it does not give the thread it's own copy; instead, all of the modifications made by the thread are made directly to the master copy.
For more info, check JLS §17, particularly §17.7 Rules for Volatile Variables.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a note: volatile is not mentioned in the exam objectives so it should not be on the test. (RHE)
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic