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.
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?
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?