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



Comment Added by : Sanjiv

Comment Added at : 2013-05-25 20:26:31

Comment on Tutorial : Transient vs Volatile modifiers in Java By Reema sen
Updated example to better show Thread T2 showing the latest updated value in variable 'testValue'. And this example does work!!

public class VolatileExmaple {
public static void main(String[] args) {

VolatileTest instanceOfVolatileExample = new VolatileTest();

Thread t1 = new Thread(instanceOfVolatileExample, "T1");
Thread t2 = new Thread(instanceOfVolatileExample, "T2");

t1.start();
t2.start();

}
}

------------------

public class VolatileTest implements Runnable {

private volatile int testValue;

public void run() {
for (int i = 0; i < 20; i++) {
try {
//System.out.println(Thread.currentThread().getName() + ": " + i);
int randomInt = new Random().nextInt(20);
if (Thread.currentThread().getName().equalsIgnoreCase("T1")) {
testValue = new Random().nextInt(20);
System.out.println("T1 - Set Test value: " + testValue);
} else if (Thread.currentThread().getName().equalsIgnoreCase("T2")) {
if (randomInt < 9) {
try {Thread.sleep(100); } catch (Exception ignore) {}
}
System.out.println("T2 - Read Test value: " + testValue + "\n");
}

Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


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. it is nice but please give simple example like are
View Tutorial          By: red at 2009-12-03 23:07:37

2. I have a question. So here I have a progress bar,
View Tutorial          By: Nick at 2011-12-06 05:10:19

3. i need help in java
View Tutorial          By: arjun tanwar at 2010-02-17 20:58:55

4. when iam trying to executing above program i am ge
View Tutorial          By: BHAGYALAXMI at 2008-10-11 06:45:57

5. Very Good Example Dude!

Note for re

View Tutorial          By: Erwin at 2009-12-06 20:20:24

6. Abstract class is a class that can not be instanti
View Tutorial          By: Abstract at 2011-07-19 01:14:09

7. This is just the web service, if you want to consu
View Tutorial          By: Carlos Kassab at 2015-02-20 17:18:06

8. Hi all,
I'm getting this error on running

View Tutorial          By: shreya.g at 2008-06-23 00:13:52

9. this site is excellent & too easy to understan
View Tutorial          By: Gaurav Kushwaha at 2010-08-16 06:30:45

10. i have to write a calculator program for very ve
View Tutorial          By: maryam at 2012-12-31 10:21:55