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 Useful tutorial for beginners.. Thanx
View Tutorial          By: Rohini at 2010-02-23 23:16:16

2. when i try to compile the two files by executing t
View Tutorial          By: Brian at 2015-08-05 12:15:37

3. It seems author has half knowlage
EJB main

View Tutorial          By: Aryan Siddharth Singh Gautam at 2010-03-22 03:40:11

4. can any plz tell me how to create a calculator in
View Tutorial          By: sam at 2010-11-13 06:53:04

5. Your code is syntactically correct, but semantical
View Tutorial          By: mobinga at 2011-07-25 15:08:27

6. Thanks
muchas gracias
Excellent

View Tutorial          By: Alfredo at 2010-05-31 09:00:36

7. thnks lot 4 helping me
View Tutorial          By: sandeep dudi at 2011-11-17 06:06:03

8. #include<iostream>
#include<conio.

View Tutorial          By: Zohaib Farooq at 2011-05-23 07:58:44

9. in statment -
new Treeset<String> (ne

View Tutorial          By: Siddhesh Khedekar at 2009-10-27 00:14:17

10. plz any one send me the code for sending and recei
View Tutorial          By: ankush at 2013-09-20 07:47:27