Previous | Next | Trail Map | Writing Java Programs | Java Objects


Declaring a Volatile Variable

When you are using threads that can modify the same variable, you can use Java's volatile keyword to indicate that to the Java runtime system. At this time, the Java runtime system does nothing with this information. 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 that a variable can be modified asynchronously by concurrent threads.

class VolatileExample {
    volatile int counter;
    . . .
}


Previous | Next | Trail Map | Writing Java Programs | Table of Contents