Wednesday, December 24, 2008

Volatile Modifier

The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program. In a multithreaded program sometimes two or more threads share the same instance variable. For efficiency considerations each thread can keep its own private copy of such a shared variable. The real copy of the variable is updated at various times, such as when synchronized method is entered. In some cases all that really matters is that the master copy of the variable always reflects its current state. To ensure this, simply specify the variable as volatile which tells the compiler that it must always use the master copy of a volatile variable.

0 comments: