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



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 want to know all things about java in a simple w
View Tutorial          By: jayanthi at 2011-12-01 08:08:56

2. Sir. Please give an example which is small and und
View Tutorial          By: mogesie gedamu at 2011-12-19 12:55:33

3. thanks a lot..please tell me how to implement this
View Tutorial          By: agnas at 2008-04-26 12:26:52

4. well example, but i have to send the received list
View Tutorial          By: amit kumar at 2011-02-21 00:43:15

5. I got error during using above code if There is ne
View Tutorial          By: Sunil at 2009-12-28 05:22:45

6. This program little hard to understand ..please gi
View Tutorial          By: sarav at 2011-02-17 05:02:46

7. sir,i m doing ME/CSE.i want to know more informati
View Tutorial          By: m.rajalakshmi at 2011-07-27 05:20:44

8. Very Nice and Perfectly Understandable.....
View Tutorial          By: rex at 2015-04-16 12:27:03

9. Good job.
View Tutorial          By: Som2ie at 2010-08-02 05:07:37

10. Good job. Very clear and concise and a great help.
View Tutorial          By: Eduardo at 2012-02-08 19:18:50