From: Parmelan, Edouard (EP510777@exchange.FRANCE.NCR.com)
Date: Wed Dec 09 1998 - 06:39:38 EST
Godmar,
> Somebody clue me in: what does "volatile" mean in Java?
> Does it have the same meaning as in C? That is, a variable whose value
> may change between subsequent reads without the current thread writing to
> it? Or a variable to which writes must be committed even thought the
> current thread doesn't read from it?
> If so, where is volatile encoded in the class file and does Kaffe's JIT
> compiler know how to handle it?
From JLS 8.3.1.4 "volatile Fields":
" Java provides a second mechanism that is more convenient
" for some purposes: a field may be declared volatile, in which
" case a thread must reconcile its working copy of the field
" with the master copy every time it accesses the variable.
" Moreover, operations on the master copies of one or more
" volatile variables on behalf of a thread are performed by the
" main memory in exactly the order that the thread requested.
you should read also JLS 17.7 "Rules for Volatile Variables"
From "The Java Tutorial" (7 July 1997):
"Declaring Volatile Variables
"
" If your class contains a member variable that is modified
" asynchronously by concurrently running threads, you can use
" Java's volatile keyword to notify the Java runtime system
" of this.
"
" The JDK 1.0 version of the Java runtime system ignores the
" volatile marker. However, future releases of the Java runtime
" system will use this information to ensure that the volatile
" variable is loaded from memory before each use, and stored to
" memory after each use thereby ensuring that the value of the
" variable is consistent and coherent within each thread.
My understanding:
Java "volatile" field modifier as the same meaning as in C.
It could be used by:
- the java compiler to not optimize access to this field (aka
not hold it in a register). It seems that pizza don't optimize
yet, but what about javac or guavac or jikes or ...
- JVM with the same behavior. For now, kaffe don't use it.
Edouard.
This archive was generated by hypermail 2b29 : Sat Sep 23 2000 - 19:57:08 EDT