commit
42360c980d
|
@ -1,55 +1,36 @@
|
||||||
package com.baeldung.concurrent.volatilekeyword;
|
package com.baeldung.concurrent.volatilekeyword;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static junit.framework.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class SharedObjectManualTest {
|
public class SharedObjectManualTest {
|
||||||
|
|
||||||
private SharedObject sharedObject;
|
|
||||||
private int valueReadByThread2;
|
|
||||||
private int valueReadByThread3;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() {
|
|
||||||
sharedObject = new SharedObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||||
|
SharedObject sharedObject = new SharedObject();
|
||||||
|
|
||||||
Thread writer = new Thread(() -> sharedObject.increamentCount());
|
Thread writer = new Thread(() -> sharedObject.increamentCount());
|
||||||
writer.start();
|
writer.start();
|
||||||
|
Thread.sleep(100);
|
||||||
|
|
||||||
Thread readerOne = new Thread(() -> {
|
Thread readerOne = new Thread(() -> {
|
||||||
try {
|
int valueReadByThread2 = sharedObject.getCount();
|
||||||
Thread.sleep(1000);
|
assertEquals(1, valueReadByThread2);
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
valueReadByThread2 = sharedObject.getCount();
|
|
||||||
});
|
});
|
||||||
readerOne.start();
|
readerOne.start();
|
||||||
|
|
||||||
Thread readerTwo = new Thread(() -> {
|
Thread readerTwo = new Thread(() -> {
|
||||||
try {
|
int valueReadByThread3 = sharedObject.getCount();
|
||||||
Thread.sleep(1000);
|
assertEquals(1, valueReadByThread3);
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
valueReadByThread3 = sharedObject.getCount();
|
|
||||||
});
|
});
|
||||||
readerTwo.start();
|
readerTwo.start();
|
||||||
|
|
||||||
assertEquals(1, valueReadByThread2);
|
|
||||||
assertEquals(1, valueReadByThread3);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||||
|
SharedObject sharedObject = new SharedObject();
|
||||||
Thread writerOne = new Thread(() -> sharedObject.increamentCount());
|
Thread writerOne = new Thread(() -> sharedObject.increamentCount());
|
||||||
writerOne.start();
|
writerOne.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
@ -58,14 +39,17 @@ public class SharedObjectManualTest {
|
||||||
writerTwo.start();
|
writerTwo.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
Thread readerOne = new Thread(() -> valueReadByThread2 = sharedObject.getCount());
|
Thread readerOne = new Thread(() -> {
|
||||||
|
int valueReadByThread2 = sharedObject.getCount();
|
||||||
|
assertEquals(2, valueReadByThread2);
|
||||||
|
});
|
||||||
readerOne.start();
|
readerOne.start();
|
||||||
|
|
||||||
Thread readerTwo = new Thread(() -> valueReadByThread3 = sharedObject.getCount());
|
Thread readerTwo = new Thread(() -> {
|
||||||
|
int valueReadByThread3 = sharedObject.getCount();
|
||||||
|
assertEquals(2, valueReadByThread3);
|
||||||
|
});
|
||||||
readerTwo.start();
|
readerTwo.start();
|
||||||
|
|
||||||
assertEquals(2, valueReadByThread2);
|
|
||||||
assertEquals(2, valueReadByThread3);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue