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. Very good explanation specially overlapped memory
View Tutorial          By: MANISH RASTOGI at 2010-06-22 05:44:07

2. It is really very gud. And wht i suggest u is if
View Tutorial          By: Ragava rao at 2008-07-19 05:33:18

3. how do i write the above same code using ajax and
View Tutorial          By: Karthik at 2011-08-29 11:21:45

4. hi.I am new in j2me and really want to learn more.
View Tutorial          By: grace at 2010-10-25 10:40:32

5. Hi Divya, Obviously some other application is usin
View Tutorial          By: Ramlak at 2008-05-01 22:16:12

6. Awesome, I am Impressed with the way the example h
View Tutorial          By: Darshan at 2010-07-09 01:34:22

7. Its working perfectly for me.Thanks so much for pr
View Tutorial          By: Sunpenn at 2009-10-13 00:53:09

8. Hi ..

I'm try to run the full samp

View Tutorial          By: Eng.A at 2009-02-21 03:11:02

9. Very nicely explained, especially the step-by-step
View Tutorial          By: Kaushik at 2012-09-20 06:58:45

10. good for us
View Tutorial          By: soumen chakraborty at 2008-08-25 01:28:30