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



Subscribe to Tutorials

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. if i want to write my gmail address how can i prov
View Tutorial          By: asmcn at 2011-12-27 19:40:49

2. Simple Codes please... I just wonder why does my p
View Tutorial          By: Joseph at 2009-11-12 05:37:50

3. some of the errors above e.g the jasper exception
View Tutorial          By: Robert at 2013-09-14 23:24:57

4. dis prgrm is not giving me any ouput..i need to re
View Tutorial          By: singh at 2015-02-19 12:16:31

5. nice its working good example.... very useful...
View Tutorial          By: Mamatha at 2012-02-02 09:23:40

6. I am using my NOKIA 2700 classic via blutooth conn
View Tutorial          By: Raheela at 2010-05-06 03:11:36

7. sir i want how to write record into file and readi
View Tutorial          By: himasekhar at 2013-05-25 10:38:31

8. Hi,
Please tell me how to execute the above

View Tutorial          By: DurgaPrasad at 2011-11-24 06:04:48

9. i am run this code changing the SMSC (ex:+9198860
View Tutorial          By: sknand13 at 2007-11-16 03:27:52

10. Hi Rajesh, you have found the right site. look at
View Tutorial          By: mohan at 2008-07-27 08:55:17