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. nice one.. i lik dis page... very informative for
View Tutorial          By: lokesh at 2013-01-10 09:06:22

2. just I am need of some of the soft copy related to
View Tutorial          By: shani at 2015-08-12 06:01:05

3. This code is only for send sms? can it also alert
View Tutorial          By: abdul kadir at 2009-10-23 05:37:46

4. i want to know the step by step process of vb.net
View Tutorial          By: moumita das at 2012-12-01 12:28:35

5. I have an error on step 0:

recipien

View Tutorial          By: xenogaz at 2009-01-28 02:43:50

6. Please assist me in getting the code to read messa
View Tutorial          By: Jaydatta at 2009-04-05 23:24:57

7. hey...i have a problem getting the system date in
View Tutorial          By: angel at 2010-04-03 09:27:38

8. sir i want this following pattern using loops

View Tutorial          By: teju at 2015-09-13 22:46:58

9. Can u show how to do text to speech with servlet.
View Tutorial          By: Rotika at 2011-01-23 20:54:40

10. hi any one can send me the code for HTTPclient pro
View Tutorial          By: indela at 2011-06-13 08:41:12