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



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. This tutorial is really good for beginners.It help
View Tutorial          By: Prabhat Kumar at 2007-04-09 05:02:02

2. very thanks... It's very useful to me...
Th

View Tutorial          By: swathi. M at 2015-01-02 16:32:19

3. Looks good, but don't use gets(). gets() is respon
View Tutorial          By: Luke at 2011-11-21 09:03:38

4. Thanks a ton sir.....
This is very informat

View Tutorial          By: Shailaja at 2009-12-27 10:43:42

5. The tutorial, it's very easy to be understood

View Tutorial          By: newcomer at 2012-04-25 03:49:35

6. Good one
View Tutorial          By: Soumya Anoop at 2010-05-13 23:23:44

7. Thks a lot..... The way of giving explanation and
View Tutorial          By: Hari at 2011-04-08 02:58:06

8. please try to elaborate the explanation with examp
View Tutorial          By: bhavitha at 2014-02-21 06:09:12

9. Thanks alot ,,, it is really more the excellent n
View Tutorial          By: SaraFaqera at 2013-12-16 16:00:07

10. Good Introduction to start j2me.thanks
View Tutorial          By: Ravinder Kasarla at 2008-12-26 06:59:29