Comment on Tutorial - Transient vs Volatile modifiers in Java By Reema sen



Comment Added by : it career

Comment Added at : 2010-02-24 00:13:50

Comment on Tutorial : Transient vs Volatile modifiers in Java By Reema sen
If you are working with the multi-threaded programming, the volatile keyword will be more useful. When multiple threads using the same variable, each thread will have its own copy of the local cache for that variable. So, when it's updating the value, it is actually updated in the local cache not in the main variable memory. The other thread which is using the same variable doesn't know anything about the values changed by the another thread. To avoid this problem, if you declare a variable as volatile, then it will not be stored in the local cache. Whenever thread are updating the values, it is updated to the main memory. So, other threads can access the updated value.

package javabeat.samples;

class ExampleThread extends Thread {
private volatile int testValue;
public ExampleThread(String str){
super(str);
}
public void run() {
for (int i = 0; i < 3; i++) {
try {
System.out.println(getName() + " : "+i);
if (getName().equals("Thread 1 "))
{
testValue = 10;
}
if (getName().equals("Thread 2 "))
{
System.out.println( "Test Value : " + testValue);
}
Thread.sleep(1000);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
}
public class VolatileExample {
public static void main(String args[]) {
new ExampleThread("Thread 1 ").start();
new ExampleThread("Thread 2 ").start();
}
}




Not Working i tried it... but


---------- run java ----------
Thread 1 : 0
Thread 2 : 0
Test Value : 0
Thread 2 : 1
Test Value : 0
Thread 1 : 1
Thread 1 : 2
Thread 2 : 2
Test Value : 0

Output completed (3 sec consumed)

<a href="http://www.itcareer.co.in">it career</a>


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. Nice Intro to start J2Me
View Tutorial          By: Mak at 2009-07-04 10:36:00

2. plz can u give me code for key excange b/n pda &am
View Tutorial          By: visnas at 2009-05-04 20:18:32

3. I'm sorry I've misunderstood, join() guarantees th
View Tutorial          By: Junkie Zhang at 2012-01-10 06:02:00

4. great!
thanks to your guide i've just under

View Tutorial          By: Chris at 2009-02-14 02:24:28

5. I am using my NOKIA 2700 via blutooth connection.
View Tutorial          By: Rahi at 2010-05-11 04:39:33

6. Can you tell me what will happen when I use a free
View Tutorial          By: Neo at 2012-04-04 16:21:35

7. i WROTE A FILE READER LIKE IT WAS IN THE SAMPLE BU
View Tutorial          By: LILI at 2011-11-06 01:47:20

8. Hi Andrew,

I have a java applicatio

View Tutorial          By: kumar at 2010-10-06 10:42:55

9. Hi, Thanks a lot for this wonderful written steps.
View Tutorial          By: Amar at 2012-09-09 17:19:58

10. I very grateful with this program it expand my ide
View Tutorial          By: Nuhu Abdullahi at 2015-06-13 11:42:38