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. Simple and easy code..........
View Tutorial          By: mandalson at 2011-12-24 04:08:12

2. Plz. help me in getting code to read messages usin
View Tutorial          By: Jaydatta at 2009-04-03 20:58:57

3. thanks..very nice and easy to understand for new j
View Tutorial          By: Ranjan at 2011-07-22 08:34:50

4. Its working ,but it overwrite the old one when run
View Tutorial          By: venkat at 2012-09-17 09:38:53

5. thanks to this site which get me understand about
View Tutorial          By: Sayak at 2012-10-08 15:57:25

6. good one
View Tutorial          By: thyagesh at 2011-12-06 05:06:27

7. thats good. but not for the begineers. because in
View Tutorial          By: jaywant topno at 2009-10-31 10:05:43

8. thank you man.
View Tutorial          By: Hariharan at 2012-08-03 01:39:16

9. what is the usage of 'detail' and 'debug' init par
View Tutorial          By: uday at 2011-07-21 04:53:02

10. hello after working with this code i got the follw
View Tutorial          By: Tapaswini Sabat at 2011-11-05 12:43:49