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 provides manna in the studies of java to me
View Tutorial          By: aravinden at 2011-12-27 08:18:18

2. How to sort an Arraylist in some given order? Say,
View Tutorial          By: Arunava Chatterjee at 2015-03-07 04:17:51

3. how to solve this problem

javax.com

View Tutorial          By: rajani at 2009-07-18 05:44:55

4. good start up fr java beginers
View Tutorial          By: sandeep Ch at 2012-01-06 07:16:34

5. plz can you help me,,,because i need this tomorrow
View Tutorial          By: ivie at 2012-12-12 11:20:35

6. useful for me
View Tutorial          By: ganga at 2010-10-06 00:51:51

7. best site ever.
View Tutorial          By: rehan ahmed at 2008-12-10 02:21:32

8. good.....not bad. .show me more programs.
View Tutorial          By: saranya at 2011-12-07 08:49:39

9. Actually, 120º *is* 2.0943951023931953 radian
View Tutorial          By: Stephen Blythe at 2012-04-02 03:39:03

10. i cannot add attachment
its saying
&

View Tutorial          By: Rintu Das at 2012-05-04 18:07:48