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. I need some help. I am a programmer - but quite n
View Tutorial          By: Ron at 2013-06-21 00:58:27

2. Very Nice Explanation.
View Tutorial          By: Anuj Bansal at 2012-08-25 12:23:59

3. it is good to unstand and easy to remember forever
View Tutorial          By: Abdul at 2011-11-10 21:03:41

4. Hi i 've used this SMSClient.java in my simple pro
View Tutorial          By: Prakah at 2011-12-22 17:10:48

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

6. thanks for the help! your tutorials really help us
View Tutorial          By: Mon Bernie Gucor at 2009-02-20 19:57:35

7. Neatly explained about the three important functio
View Tutorial          By: Jitendra Lenka at 2011-02-03 06:34:10

8. programming for java is excelent
View Tutorial          By: roldan at 2010-07-19 00:22:52

9. hi,
my problem is.......
i use vb.ne

View Tutorial          By: Anirban at 2009-10-25 10:15:13

10. I am using VS2010 & SAP for crystal report. I
View Tutorial          By: Abdullah Al Mamun at 2011-08-11 23:20:37