Skip to content

Commit 99eee4d

Browse files
baljeet20maibin
authored andcommitted
BAEL-1077 Guide to volatile keyword (eugenp#2433)
* review changes * BAEL-1024 Removed the proxy reference * BAEL-1024 Renamed methods * BAEL-1077 Guide to volatile keyword
1 parent c39abbf commit 99eee4d

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.concurrent.volatilekeyword;
2+
3+
4+
public class SharedObject {
5+
private volatile int count=0;
6+
7+
public void increamentCount(){
8+
count++;
9+
}
10+
public int getCount(){
11+
return count;
12+
}
13+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.baeldung.concurrent.volatilekeyword;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class SharedObjectManualTest {
10+
11+
SharedObject sharedObject;
12+
int valueReadByThread2;
13+
int valueReadByThread3;
14+
15+
@Before
16+
public void setUp(){
17+
sharedObject = new SharedObject();
18+
}
19+
20+
@Test
21+
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
22+
Thread writer = new Thread(){
23+
public void run(){
24+
sharedObject.increamentCount();
25+
}
26+
};
27+
writer.start();
28+
29+
30+
Thread readerOne = new Thread(){
31+
public void run(){
32+
try {
33+
Thread.sleep(1000);
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
valueReadByThread2= sharedObject.getCount();
38+
}
39+
};
40+
readerOne.start();
41+
42+
Thread readerTwo = new Thread(){
43+
public void run(){
44+
try {
45+
Thread.sleep(1000);
46+
} catch (InterruptedException e) {
47+
e.printStackTrace();
48+
}
49+
valueReadByThread3=sharedObject.getCount();
50+
}
51+
};
52+
readerTwo.start();
53+
54+
assertEquals(1,valueReadByThread2);
55+
assertEquals(1,valueReadByThread3);
56+
57+
}
58+
59+
@Test
60+
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
61+
Thread writerOne = new Thread(){
62+
public void run(){
63+
sharedObject.increamentCount();
64+
}
65+
};
66+
writerOne.start();
67+
Thread.sleep(100);
68+
69+
Thread writerTwo = new Thread(){
70+
public void run(){
71+
sharedObject.increamentCount();
72+
}
73+
};
74+
writerTwo.start();
75+
Thread.sleep(100);
76+
77+
Thread readerOne = new Thread(){
78+
public void run(){
79+
valueReadByThread2= sharedObject.getCount();
80+
}
81+
};
82+
readerOne.start();
83+
84+
Thread readerTwo = new Thread(){
85+
public void run(){
86+
valueReadByThread3=sharedObject.getCount();
87+
}
88+
};
89+
readerTwo.start();
90+
91+
assertEquals(2,valueReadByThread2);
92+
assertEquals(2,valueReadByThread3);
93+
94+
}
95+
@After
96+
public void cleanup(){
97+
sharedObject = null;
98+
}
99+
}

0 commit comments

Comments
 (0)