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. very good explanation for Include Action in struts
View Tutorial          By: VIshwanath at 2011-03-21 06:00:22

2. can i apply multidimensional array list the progra
View Tutorial          By: [email protected] at 2009-07-01 05:09:27

3. great answer...
View Tutorial          By: nagi ponnagani at 2011-04-29 14:11:21

4. we need to know how to write this coding..

View Tutorial          By: lalala at 2009-07-15 20:35:16

5. ' REFERENCIA EN EL PROYECTO (Microsoft Excel 12.0
View Tutorial          By: Anonymous at 2015-04-21 03:10:41

6. How to find port names in pc? like COM1 etc ? i wn
View Tutorial          By: layiba at 2013-12-25 08:28:30

7. Can u please help me to implement traversing forw
View Tutorial          By: Rahi at 2015-09-18 11:12:32

8. I Want To Know More About Windows Azure Advantages
View Tutorial          By: sathish at 2010-08-31 10:23:10

9. Thanks for the code it save's me a lot of work, ho
View Tutorial          By: Wishkah at 2009-09-04 00:29:16

10. Very good !!
View Tutorial          By: SGK at 2009-04-26 23:53:44