Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Volatile

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is volatile and it's use?

Thanks in advance,
ADS
 
Eric Edwards
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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!
 
Gina Pandher
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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.

 
Binod
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the code is not compiling when checkVar is
declared as:
volatile boolean checkVar = true;
Any idea?
Binod.
 
Steve Fahlbusch
Bartender
Posts: 602
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • 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>
 
Deenesh Daw
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An explicit description is given at
http://www.javaperformancetuning.com/news/qotm030.shtml
 
David Miranda
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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?
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • 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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This doen't particularly sound like beginner's stuff, so I'm moving this to the intermediate forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic