Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces


Declaring a Volatile Variable

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.

At this time, the Java runtime system ignores the volatile marker. However, future releases of the Java runtime system will use this information to ensure that the variable is loaded from memory before each use, and stored to memory after each use to ensure that the value of the variable is consistent and coherent within each thread.

The following variable declaration is an example of how to declare that a variable can be modified asynchronously by concurrent threads.

class VolatileExample {
    volatile int counter;
    . . .
}


Previous | Next | Trail Map | Writing Java Programs | Objects, Classes, and Interfaces