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. ok good.But what about plug-in's Information.
View Tutorial          By: Ramarao at 2010-01-31 07:32:02

2. how to run this code....... Please tell somebody m
View Tutorial          By: Honey at 2015-01-24 14:27:38

3. steps 2 create and use tiles
View Tutorial          By: sasank at 2008-10-03 08:06:43

4. this is a site site for our problem to be sorted o
View Tutorial          By: Atique at 2009-11-02 00:15:18

5. Hi
I am newly in java. I have one dought.

View Tutorial          By: Dinakar at 2008-07-26 01:13:08

6. I want de total main prg to send and receive... se
View Tutorial          By: sharu at 2011-09-12 16:49:24

7. i get exception NoSuchPortException. i tried on di
View Tutorial          By: harryjoy at 2009-11-22 05:52:48

8. This site is an Excellent Choice for lerners.Thank
View Tutorial          By: Sravanthi at 2011-06-28 06:20:35

9. hi, Thanks to the post.
I am stuck on step

View Tutorial          By: david_david at 2010-08-21 01:10:19

10. hello sir , i m student of mca . i am working on S
View Tutorial          By: Pallavi at 2013-04-06 13:53:54