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. I Want more Advanced Case Shown...
I Know E

View Tutorial          By: Electron at 2009-11-23 02:32:07

2. Finally! You enlighted me in a small problem: it w
View Tutorial          By: Fernando Martin at 2011-02-25 12:54:58

3. I think the programmer overlooked that "this
View Tutorial          By: Joe Schmoe at 2010-10-16 00:01:27

4. Hey thanks for your tutorial. But i'm getting an e
View Tutorial          By: acc at 2013-07-25 14:30:50

5. It is really very gud. And wht i suggest u is if
View Tutorial          By: Ragava rao at 2008-07-19 05:33:18

6. Hi. When i click on Install certificate. It tells
View Tutorial          By: jaris at 2011-10-18 11:00:41

7. Gives a syntax error
View Tutorial          By: Siddharth at 2015-06-10 05:15:00

8. Is it possible to create a Reader class without lo
View Tutorial          By: bs at 2010-10-11 05:42:34

9. This is a good written article!
It gave me

View Tutorial          By: Anders Branderud at 2010-01-10 03:24:25

10. Fantastic work guys
View Tutorial          By: wizard at 2012-09-22 04:18:59