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. I am getting error like this:
javax.comm:

View Tutorial          By: Santhosh Samban at 2015-01-06 13:47:39

2. If the database server goes down, should the conne
View Tutorial          By: Naveen Gowd at 2012-02-02 07:17:38

3. very very useful..thank you
View Tutorial          By: sridaran at 2009-03-21 00:45:21

4. nice one dear
View Tutorial          By: pradeep at 2010-02-15 07:00:35

5. i tried, the coding dont hv any errors but its not
View Tutorial          By: anna at 2010-11-02 21:56:27

6. super explaination
View Tutorial          By: santhish at 2011-07-06 05:00:54

7. can someone explain above example in detail
View Tutorial          By: neha at 2012-03-24 05:57:34

8. good information
View Tutorial          By: varun at 2013-01-04 14:51:19

9. Yes the article is good and useful...!
View Tutorial          By: Regalla Naresh Reddy at 2009-04-12 23:16:24

10. good....
View Tutorial          By: Rini at 2013-01-08 10:44:54