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. nice explainations for the beginners like me :)
View Tutorial          By: Divya Taneja at 2012-02-11 13:17:17

2. it was so nice .................i can easily solve
View Tutorial          By: pravin at 2012-07-14 06:15:33

3. i need java simple programs
View Tutorial          By: Tharani at 2010-01-22 06:39:25

4. Hi,
I've tried this but got a problem. Firs

View Tutorial          By: Hendrik Jabs at 2009-12-10 02:46:13

5. 'Numbers' is the name of his table, you need to su
View Tutorial          By: Amir at 2010-09-15 10:59:20

6. Thanks. Your post was clear and worked perfectly a
View Tutorial          By: Suma at 2009-03-05 09:40:08

7. Thanks to OP and Zarno for finishing off this code
View Tutorial          By: Soapy Balls at 2011-05-27 05:26:13

8. great material for language bugs....
View Tutorial          By: manoj kapri at 2009-11-30 00:30:13

9. I'm having trouble figuring out how to sign my mid
View Tutorial          By: Patrick Somers at 2011-04-04 14:54:45

10. This is very useful for beginners.
I want f

View Tutorial          By: shankar at 2013-02-04 13:28:16