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



Comment Added by : Prithvi

Comment Added at : 2010-01-07 10:24:12

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();
}
}


View Tutorial



Related Tutorials

Java program to get location meta data from an image

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

Archived Comments

1. Hi,

I tried this script with noki

View Tutorial          By: sravan at 2009-07-11 11:17:45

2. I am deveolper in envision .i am interested in scj
View Tutorial          By: shiva kumar at 2013-02-20 05:51:42

3. sir i am mca student. i can choose my flatform is
View Tutorial          By: sridevi at 2012-06-06 10:21:39

4. my question is the same of sajjad.what about (!iss
View Tutorial          By: hossain at 2012-04-26 04:28:56

5. iam getting intrest to watch this site
View Tutorial          By: monika at 2013-02-15 15:48:57

6. Hi, i am trying this code but at compile time it w
View Tutorial          By: swapnil at 2012-12-16 01:37:57

7. thank you
View Tutorial          By: latif mohammad khan at 2013-01-18 09:53:18

8. Explanation is very good. Thanks all.
View Tutorial          By: reddy at 2010-04-07 08:47:09

9. It won't work for me ;S
View Tutorial          By: jordi at 2011-01-21 08:22:45

10. how doeas peek work in a c++ program
View Tutorial          By: odwa at 2008-02-29 02:44:19