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

Java program to get location meta data from an image

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

Archived Comments

1. Thank you sir this is a nice example/
View Tutorial          By: Techbymak at 2013-01-08 09:51:54

2. Hi can anyone tell me how to use try catch in comm
View Tutorial          By: Gossip Lanka at 2015-04-26 05:00:44

3. crystal clear explanation. He's explained very nic
View Tutorial          By: jay at 2009-12-08 10:28:14

4. man i really havnt got your last example. how it g
View Tutorial          By: rakesh at 2013-02-21 08:20:29

5. How to download any file when a button is in .jsp
View Tutorial          By: Gopesh at 2012-04-13 09:35:11

6. Thanks a ton! :)
View Tutorial          By: Prateek at 2015-03-12 11:40:08

7. hi guys.
i have downloaded the code for sen

View Tutorial          By: Tabrez Ansari at 2012-09-13 11:03:46

8. how to get only the year like 2012
View Tutorial          By: Iguster at 2012-01-27 22:20:32

9. Whenever i run this program, it terminates immedia
View Tutorial          By: Gopi at 2015-08-19 06:55:41

10. thanx very much...
View Tutorial          By: Rajesh at 2015-10-09 08:57:36