• 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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is volatile and it's use?

Thanks in advance,
ADS
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ADS:
What is volatile and it's use?

Thanks in advance,
ADS


Would you please be more specific.
Thanks!
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Volatile is one of the reserved keywords in java.
It indicates that variables may change out unexpectedly.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric and Gina,
Thanks a lot!
I want to be more specify about the use of Volatile with Example..

Thanks again,
ADS

Originally posted by ADS:
What is volatile and it's use?

Thanks in advance,
ADS


 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I guess this can explain 'volatile' a bit :

Consider the following snippet:

boolean checkVar = true;
int i = 0;
while ( checkVar )
{
i++;
}

we know that this is going to be an infinite loop since we are not changing checkVar to false anywhere within the loop.
On the other hand, if we declare checkVar as :

volatile boolean checkVar = true;
the case is different.
Without the use of volatile, java is free to optimize the loop in such a way that the value of the variable is held in a register of the CPu and not necessarily reexamined with each iteration. The use of volatile prevents this optimization, telling Java that checkVar may change in ways not directly apparent in the intermediate code ( as in some other thread... )
This explanation is from the book Beginning Java 2 - Ivor Horton
pg. 292
Hope this helps.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the code is not compiling when checkVar is
declared as:
volatile boolean checkVar = true;
Any idea?
Binod.
 
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code:
<pre>
public class Volitile {

public static boolean checkVar = true;
public static void main ( String[] args ) {
int i = 0;
while ( checkVar ) {
i++;
}
}
}
</pre>
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An explicit description is given at
http://www.javaperformancetuning.com/news/qotm030.shtml
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So....

1) volatile is only used in threading?

and

2) all thread objects created from a thread class that contains a volatile variable will synchronize the value of that volatile variable for all threads whenever that value is updated?

The volatile modifier is essentially the same thing as the static modifier, but for threads?
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Miranda:
So....
1) volatile is only used in threading?



Using of volatile keyword forces additional restricts on optimization. When you use volatile keyword, you just say to compiler "hey, man, this variable can be changed by more then one thread. So, be carefully with it."
There is no sense in using volatile kayword when you have only one thread.

Originally posted by David Miranda:
2) all thread objects created from a thread class that contains a volatile variable will synchronize the value of that volatile variable for all threads whenever that value is updated?


Not exactly.

Not only thread class can hold volatile variables. Any class can hold it.
You can read more about volatile here

Originally posted by David Miranda:
The volatile modifier is essentially the same thing as the static modifier, but for threads?



No.
[ March 18, 2005: Message edited by: George Bolyuba ]
 
David Miranda
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



quote:
--------------------------------------------------------------------------------
Originally posted by David Miranda:
2) all thread objects created from a thread class that contains a volatile variable will synchronize the value of that volatile variable for all threads whenever that value is updated?

--------------------------------------------------------------------------------


Not exactly.

Not only thread class can hold volatile variables. Any class can hold it.
You can read more about volatile here



Thanks for the reply George,

Okay, so other classes besides the thread class can hold the volatile modifier, but do other classes besides the thread class actually use the volatile modifier for non-threading purposes? If so, what do these non-threading-related classes use the volatile modifier for?

Also, am I correct in my understanding that threads who share a volatile variable will synchronize the value of that volatile variable will all threads who use it? So if three thread objects (objA, objB, and objC) are created from a thread class that has a int volatile variable thats initialized to, say 4; and objC changes its value of its volatile variable to 3, objects objA and objB's value of the volatile variable will also be 3?
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides Java threads, I think that native methods can affect variables outside the jvm in ways that require volatile declarations.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This doen't particularly sound like beginner's stuff, so I'm moving this to the intermediate forum.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic