volatile keyword in Java best explanation
volatile Keyword use to maintain a variable in Main memory.
Suppose that two threads are working on text.i
If two threads run on different processors, If one thread modifies its value the change reflect to other threads and in the original main memory.
volatile variables are not create local copy.
volatile would not be sufficient to make thread-safe, synchronization would still be needed.
Using volatile variables reduces the risk of memory, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable.
This means that changes to a volatile variable are always visible to other threads.
read more about mutual exclusion, visibility and Atomic.
class text
{
static int i= 6;
}
Suppose that two threads are working on text.i
If two threads run on different processors, If one thread modifies its value the change reflect to other threads and in the original main memory.
volatile variables are not create local copy.
volatile would not be sufficient to make thread-safe, synchronization would still be needed.
Using volatile variables reduces the risk of memory, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable.
This means that changes to a volatile variable are always visible to other threads.
read more about mutual exclusion, visibility and Atomic.
Comments
Post a Comment