This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
Joined: Sep 21, 2000
Posts: 19
posted
0
Volatile is one of the reserved keywords in java. It indicates that variables may change out unexpectedly.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
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
Joined: Sep 18, 2000
Posts: 208
posted
0
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.
Regards,<BR>Shree
Binod
Greenhorn
Joined: Sep 19, 2000
Posts: 7
posted
0
But the code is not compiling when checkVar is declared as: volatile boolean checkVar = true; Any idea? Binod.
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
Joined: Feb 18, 2005
Posts: 162
posted
0
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 ]
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 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
Joined: Mar 13, 2004
Posts: 1272
posted
0
Besides Java threads, I think that native methods can affect variables outside the jvm in ways that require volatile declarations.
Mike Gershman
SCJP 1.4, SCWCD in process
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
posted
0
This doen't particularly sound like beginner's stuff, so I'm moving this to the intermediate forum.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.