aspose file tools*
The moose likes Java in General and the fly likes Volatile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Volatile" Watch "Volatile" New topic
Author

Volatile

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What is volatile and it's use?

Thanks in advance,
ADS
Eric Edwards
Ranch Hand

Joined: Feb 12, 2000
Posts: 60
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
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
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
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
But the code is not compiling when checkVar is
declared as:
volatile boolean checkVar = true;
Any idea?
Binod.
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 555
    
    7

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

Joined: Nov 07, 2002
Posts: 2
An explicit description is given at
http://www.javaperformancetuning.com/news/qotm030.shtml
David Miranda
Ranch Hand

Joined: Mar 14, 2005
Posts: 34
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

Joined: Feb 18, 2005
Posts: 162
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 ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
David Miranda
Ranch Hand

Joined: Mar 14, 2005
Posts: 34


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

Joined: Mar 13, 2004
Posts: 1272
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
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Volatile
 
Similar Threads
another thread question
volatile
volatile and transient Details
volatile keyword
Volatile