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. how to code for images hitting in jsp like googl
View Tutorial          By: Kumaravel at 2014-08-06 11:23:08

2. great work
View Tutorial          By: Dinesh557 at 2008-09-25 00:27:59

3. So I followed you steps with no compile errors. W
View Tutorial          By: PointLookoutResident at 2014-10-14 19:01:11

4. Thank You , It's Very Good.
View Tutorial          By: Jack at 2012-06-10 14:09:24

5. I don't get it... there seem to be only two differ
View Tutorial          By: bibiki at 2012-04-05 02:01:32

6. is it possible to change the increment variable?
View Tutorial          By: Lundi at 2013-04-08 21:21:13

7. sir,
When I run the above code and give 6 o

View Tutorial          By: nishant at 2012-07-01 17:11:20

8. example is good for understand what is the functio
View Tutorial          By: Ankit Shukla at 2009-09-17 15:25:17

9. Seriously very nice explanation.... Thank You
View Tutorial          By: Pulkit agrawal at 2012-03-20 10:28:10

10. can any body help me,i need to send a sms from sim
View Tutorial          By: vinayaka DJ at 2011-11-14 12:44:01