BAEL-1077 Guide to volatile keyword (#2433)
* review changes * BAEL-1024 Removed the proxy reference * BAEL-1024 Renamed methods * BAEL-1077 Guide to volatile keyword
This commit is contained in:
parent
c39abbf4eb
commit
99eee4df2c
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
|
||||
public class SharedObject {
|
||||
private volatile int count=0;
|
||||
|
||||
public void increamentCount(){
|
||||
count++;
|
||||
}
|
||||
public int getCount(){
|
||||
return count;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package com.baeldung.concurrent.volatilekeyword;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
public class SharedObjectManualTest {
|
||||
|
||||
SharedObject sharedObject;
|
||||
int valueReadByThread2;
|
||||
int valueReadByThread3;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
sharedObject = new SharedObject();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||
Thread writer = new Thread(){
|
||||
public void run(){
|
||||
sharedObject.increamentCount();
|
||||
}
|
||||
};
|
||||
writer.start();
|
||||
|
||||
|
||||
Thread readerOne = new Thread(){
|
||||
public void run(){
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
valueReadByThread2= sharedObject.getCount();
|
||||
}
|
||||
};
|
||||
readerOne.start();
|
||||
|
||||
Thread readerTwo = new Thread(){
|
||||
public void run(){
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
valueReadByThread3=sharedObject.getCount();
|
||||
}
|
||||
};
|
||||
readerTwo.start();
|
||||
|
||||
assertEquals(1,valueReadByThread2);
|
||||
assertEquals(1,valueReadByThread3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||
Thread writerOne = new Thread(){
|
||||
public void run(){
|
||||
sharedObject.increamentCount();
|
||||
}
|
||||
};
|
||||
writerOne.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread writerTwo = new Thread(){
|
||||
public void run(){
|
||||
sharedObject.increamentCount();
|
||||
}
|
||||
};
|
||||
writerTwo.start();
|
||||
Thread.sleep(100);
|
||||
|
||||
Thread readerOne = new Thread(){
|
||||
public void run(){
|
||||
valueReadByThread2= sharedObject.getCount();
|
||||
}
|
||||
};
|
||||
readerOne.start();
|
||||
|
||||
Thread readerTwo = new Thread(){
|
||||
public void run(){
|
||||
valueReadByThread3=sharedObject.getCount();
|
||||
}
|
||||
};
|
||||
readerTwo.start();
|
||||
|
||||
assertEquals(2,valueReadByThread2);
|
||||
assertEquals(2,valueReadByThread3);
|
||||
|
||||
}
|
||||
@After
|
||||
public void cleanup(){
|
||||
sharedObject = null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue