Comment on Tutorial - Transient vs Volatile modifiers in Java By Reema sen
Comment Added by : aayush
Comment Added at : 2009-11-16 07:38:23
Comment on Tutorial : Transient vs Volatile modifiers in Java By Reema sen
A volatile variable does not have a copy maintained in the local memory of the thread (on the stack). All changes to the volatile variable (caused by multiple threads) are flushed out to the heap memory (visible from all threads). Hence volatile variable values remain consistent for all threads.
On the other hand, for other instance variables, each java thread maintains a local copy on the stack. Multiple threads may modify this local copy of the instance variable and hence inconsistent values may be visible for multiple threads.
For preventing this condition, we synchronize. During synchronization, a lock is first taken on the object monitor. Then the thread reads the state from the main memory and flushes its internal state. Subsequently, the synchronized code block/method code is executed. Once the execution completes, all the changes to the variables of that thread are flushed out to the main memory. Then the object monitor lock is released.
So, as we can see, volatile is a specialized case of synchronization. The only exceptions are, that it operates on a single field and no locks on the object monitor are required (as it operates on the heap memory and not the thread local stack memory).
- Data Science
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews
Subscribe to Tutorials
3. I need the multiple record store RMS code.Pls help
View Tutorial By: Raghuvir at 2009-05-13 23:03:44
View Tutorial By: Mashoud at 2008-05-04 02:54:38
5. realy i enjoy it for visit your site. and i unders
View Tutorial By: vinod balot at 2015-02-09 17:43:30
7. how to connect the database in servelet use to sta
View Tutorial By: sudhir singh tomar at 2011-04-28 03:10:34
8. Please, I need help, how can i write a program &qu
View Tutorial By: LULU at 2012-11-23 01:34:07