Comment on Tutorial - Transient vs Volatile modifiers in Java By Reema sen



Comment Added by : it career

Comment Added at : 2010-02-24 00:13:50

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();
}
}




Not Working i tried it... but


---------- run java ----------
Thread 1 : 0
Thread 2 : 0
Test Value : 0
Thread 2 : 1
Test Value : 0
Thread 1 : 1
Thread 1 : 2
Thread 2 : 2
Test Value : 0

Output completed (3 sec consumed)

<a href="http://www.itcareer.co.in">it career</a>


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. sir, thanks sir. very help to me. its simple and e
View Tutorial          By: kannathasan.M at 2012-10-01 07:59:28

2. very useful article. every jsp resource will get g
View Tutorial          By: pechirani at 2011-06-29 04:00:49

3. Your solution is really helped me. Thanks for your
View Tutorial          By: Rajakumar at 2010-05-31 06:05:53

4. Thanks,
But i would like to use an array li

View Tutorial          By: Gustavo at 2011-06-15 21:03:20

5. hi .the program does run thank you for that ,i nee
View Tutorial          By: mazen at 2008-09-09 04:55:16

6. Please send me working code and setup,,My mail id
View Tutorial          By: Karthik at 2012-03-30 19:56:35

7. very nice but i want some more exapmles
View Tutorial          By: prasad lengare at 2010-10-03 23:12:47

8. Getting coding from this is very easy to me...
View Tutorial          By: shanmugaraja at 2009-09-09 01:50:02

9. Dear author
u missed check condition for 0(

View Tutorial          By: Maroof Ahmad at 2012-03-28 14:59:53

10. I changed import javax.comm.*; for this line impo
View Tutorial          By: Julen at 2010-07-18 18:39:25