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. plz send me 1 ex that will explain how to use conn
View Tutorial          By: abhijit marne at 2011-12-09 06:32:07

2. i nice programs tks
View Tutorial          By: jai at 2011-05-09 14:09:38

3. //EXAMPLE OF NESTED IF*****
//FIND OUT BIGE

View Tutorial          By: Amitava Misra at 2009-11-03 06:11:33

4. Nice article .... Thank you...
View Tutorial          By: viswa at 2012-06-16 17:21:59

5. what is self-executing program ? or dynamic data
View Tutorial          By: vasu at 2013-04-18 17:36:35

6. hai...
i am completed MCA am good at java.

View Tutorial          By: HEMALATHA at 2013-08-03 10:04:57

7. Can u tell me more about struts related to databas
View Tutorial          By: archana at 2010-09-15 04:38:54

8. Actually in gallery example the images can be move
View Tutorial          By: santu at 2013-02-16 12:40:29

9. Very informative article, this is the exact articl
View Tutorial          By: Abhinav bindra at 2012-03-23 14:00:12

10. thanks for this tutorial
View Tutorial          By: d3ptzz at 2009-08-03 23:38:57