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. can i get some basic examples of java program
View Tutorial          By: raja at 2013-06-04 10:47:12

2. Dear Sir,

This code is so good. but

View Tutorial          By: Jai Singh Saini at 2010-05-06 06:13:45

3. hi
i try to write a code for send data or s

View Tutorial          By: mjh at 2011-02-16 08:26:10

4. we are getting an error saying that " the sta
View Tutorial          By: chaitra at 2014-03-20 10:53:02

5. Hi Robert,

The precision statement

View Tutorial          By: George at 2015-01-31 06:25:18

6. Your Work is Good, But i want to ask if there is a
View Tutorial          By: Good Muyis at 2011-01-08 09:47:14

7. At my phone number, it showing

5 re

View Tutorial          By: Zafar Iqbal at 2013-04-02 19:48:32

8. Good explaination, easy to understand
thank

View Tutorial          By: channaveer at 2010-05-02 05:03:21

9. Hi,
i am working on ocap xlet project. here

View Tutorial          By: swati at 2012-07-09 08:39:13

10. great work
View Tutorial          By: Bhaskaran at 2013-01-21 11:23:19