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



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. its great!!!!
View Tutorial          By: safrgdg at 2015-02-12 10:10:07

2. i know this example this is in mg.hill but i want
View Tutorial          By: srikanta at 2009-11-18 00:06:57

3. Thnaks a lot. Deep in brief.
View Tutorial          By: Thanks at 2012-05-23 03:36:37

4. Nice one.your example proves that if you are apply
View Tutorial          By: Nisarg pathak at 2015-09-30 17:23:37

5. thnkss buddyy its very helpful for my assignmentss
View Tutorial          By: yamini at 2011-02-25 21:01:23

6. nice article... got th topic clearly
View Tutorial          By: praba at 2011-05-09 23:29:26

7. This is the helpful Site for beginner....!!!!!!!!!
View Tutorial          By: Anup at 2013-07-26 13:40:49

8. Excellent Example...... To understand the concept
View Tutorial          By: SUNNY at 2009-02-24 23:41:04

9. Hi Paul,
It is a very interesting and help

View Tutorial          By: Therese at 2010-04-28 05:36:09

10. i apprciate your work,plz keep on working harder a
View Tutorial          By: Raymond B. Benedicto at 2013-01-12 07:08:59