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. yes 29 you are correct! 27's example is prone to a
View Tutorial          By: sameendra at 2011-07-24 11:55:54

2. I want java
View Tutorial          By: A.S.Vtihyatharan at 2009-12-01 03:25:36

3. plz i want afull code to send sms to mobile includ
View Tutorial          By: alosh at 2010-10-17 08:39:25

4. Hello Sir/Madam, I want to know the basic concepts
View Tutorial          By: venkatesh at 2012-12-24 06:17:00

5. THNX RAMLAK..
HEY RAMLAK I HAVE A LOT OF DO

View Tutorial          By: Vinod at 2009-01-02 22:04:40

6. i searched a lot of sites but found the answer her
View Tutorial          By: ALIEN ATTACK at 2012-01-09 09:39:36

7. Sir, this is very helpful for students and profess
View Tutorial          By: Pankaj Choudhary at 2010-10-19 00:24:36

8. Hi Ramlak,

I tried to use the datab

View Tutorial          By: Braj at 2009-02-07 03:31:13

9. The basic structure of simple program is used to l
View Tutorial          By: Muthusasi at 2009-12-17 21:10:01

10. Fantastic yaar .realy very good n simple example..
View Tutorial          By: Satya at 2012-10-11 09:33:17