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. Problem
The managed bean doesn't get update

View Tutorial          By: jsfuser at 2007-12-25 22:21:54

2. if the "extend" word is not present does
View Tutorial          By: detteskii at 2011-06-27 09:12:23

3. one have to store .wav file in res folder of your
View Tutorial          By: swetha at 2013-06-14 06:47:57

4. Awesome, now I can say I've fully understood volat
View Tutorial          By: Andrew at 2012-01-13 18:19:15

5. now I know what isset is for, thank you very much
View Tutorial          By: Anonymous at 2010-12-23 20:43:29

6. hi.I am new in j2me and really want to learn more
View Tutorial          By: A.vinothKumar at 2012-08-20 07:50:26

7. i have using struts tag in jsp ,
i have us

View Tutorial          By: sachin khatode at 2008-11-11 01:24:33

8. hey plzz help me the code is not working for me...
View Tutorial          By: kuldeep at 2014-02-24 18:16:59

9. Thanks for this nice comparision table for Spring
View Tutorial          By: guddu at 2009-10-17 03:08:59

10. thanx to schildt
View Tutorial          By: singh at 2012-10-26 11:57:11