commit
cabff07ff2
|
@ -4,7 +4,7 @@ package com.baeldung.concurrent.volatilekeyword;
|
||||||
public class SharedObject {
|
public class SharedObject {
|
||||||
private volatile int count=0;
|
private volatile int count=0;
|
||||||
|
|
||||||
void increamentCount(){
|
void incrementCount(){
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
public int getCount(){
|
public int getCount(){
|
||||||
|
|
|
@ -10,7 +10,7 @@ public class SharedObjectManualTest {
|
||||||
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||||
SharedObject sharedObject = new SharedObject();
|
SharedObject sharedObject = new SharedObject();
|
||||||
|
|
||||||
Thread writer = new Thread(() -> sharedObject.increamentCount());
|
Thread writer = new Thread(() -> sharedObject.incrementCount());
|
||||||
writer.start();
|
writer.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
@ -31,11 +31,11 @@ public class SharedObjectManualTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException {
|
||||||
SharedObject sharedObject = new SharedObject();
|
SharedObject sharedObject = new SharedObject();
|
||||||
Thread writerOne = new Thread(() -> sharedObject.increamentCount());
|
Thread writerOne = new Thread(() -> sharedObject.incrementCount());
|
||||||
writerOne.start();
|
writerOne.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
Thread writerTwo = new Thread(() -> sharedObject.increamentCount());
|
Thread writerTwo = new Thread(() -> sharedObject.incrementCount());
|
||||||
writerTwo.start();
|
writerTwo.start();
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue