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
Hello,

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).


View Tutorial



Related Tutorials

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Java program using Method Overriding

Archived Comments

1. haiiiiiiiii sir , i have error in this program, er
View Tutorial          By: Vinod at 2012-07-31 12:23:19

2. could anyone run this program on Netbean? If there
View Tutorial          By: Wei Long at 2008-06-16 19:23:48

3. where is the main class dear.................
View Tutorial          By: akhand at 2011-03-01 23:58:05

4. when i was running this code... it is giving me th
View Tutorial          By: Nilesh Agrawal at 2012-06-08 15:08:25

5. Can you provide some more specific application of
View Tutorial          By: Rushikesh at 2012-01-15 18:23:36

6. Good ,I like here! I send gmail with j
View Tutorial          By: jackhexl at 2009-04-02 03:29:44

7. good.....not bad. .show me more programs.
View Tutorial          By: saranya at 2011-12-07 08:49:39

8. know someone how to compare various objects?

View Tutorial          By: Peter at 2009-10-07 07:32:31

9. hi....!
This program is working well in co

View Tutorial          By: param at 2014-10-15 07:02:49

10. thanks! great tutorial!
View Tutorial          By: Gracia at 2013-01-10 07:58:06