From 6db067d2c9d433828d49cd4d697ddc145019b433 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 15 Aug 2017 15:17:24 +0200 Subject: [PATCH] Refactor volatile (#2444) --- .../volatilekeyword/SharedObject.java | 2 +- .../SharedObjectManualTest.java | 82 ++++++------------- 2 files changed, 28 insertions(+), 56 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java b/core-java/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java index 3f24df5059..063c835481 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java +++ b/core-java/src/main/java/com/baeldung/concurrent/volatilekeyword/SharedObject.java @@ -4,7 +4,7 @@ package com.baeldung.concurrent.volatilekeyword; public class SharedObject { private volatile int count=0; - public void increamentCount(){ + void increamentCount(){ count++; } public int getCount(){ diff --git a/core-java/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java b/core-java/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java index 260a7c060b..8770cb4e90 100644 --- a/core-java/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java +++ b/core-java/src/test/java/com/baeldung/concurrent/volatilekeyword/SharedObjectManualTest.java @@ -8,92 +8,64 @@ import static junit.framework.Assert.assertEquals; public class SharedObjectManualTest { - SharedObject sharedObject; - int valueReadByThread2; - int valueReadByThread3; + private SharedObject sharedObject; + private int valueReadByThread2; + private int valueReadByThread3; @Before - public void setUp(){ + public void setUp() { sharedObject = new SharedObject(); } @Test public void whenOneThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException { - Thread writer = new Thread(){ - public void run(){ - sharedObject.increamentCount(); - } - }; + Thread writer = new Thread(() -> sharedObject.increamentCount()); writer.start(); - Thread readerOne = new Thread(){ - public void run(){ - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - valueReadByThread2= sharedObject.getCount(); + Thread readerOne = new Thread(() -> { + 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(); + Thread readerTwo = new Thread(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); } - }; + valueReadByThread3 = sharedObject.getCount(); + }); readerTwo.start(); - assertEquals(1,valueReadByThread2); - assertEquals(1,valueReadByThread3); + assertEquals(1, valueReadByThread2); + assertEquals(1, valueReadByThread3); } @Test public void whenTwoThreadWrites_thenVolatileReadsFromMainMemory() throws InterruptedException { - Thread writerOne = new Thread(){ - public void run(){ - sharedObject.increamentCount(); - } - }; + Thread writerOne = new Thread(() -> sharedObject.increamentCount()); writerOne.start(); Thread.sleep(100); - Thread writerTwo = new Thread(){ - public void run(){ - sharedObject.increamentCount(); - } - }; + Thread writerTwo = new Thread(() -> sharedObject.increamentCount()); writerTwo.start(); Thread.sleep(100); - Thread readerOne = new Thread(){ - public void run(){ - valueReadByThread2= sharedObject.getCount(); - } - }; + Thread readerOne = new Thread(() -> valueReadByThread2 = sharedObject.getCount()); readerOne.start(); - Thread readerTwo = new Thread(){ - public void run(){ - valueReadByThread3=sharedObject.getCount(); - } - }; + Thread readerTwo = new Thread(() -> valueReadByThread3 = sharedObject.getCount()); readerTwo.start(); - assertEquals(2,valueReadByThread2); - assertEquals(2,valueReadByThread3); + assertEquals(2, valueReadByThread2); + assertEquals(2, valueReadByThread3); } - @After - public void cleanup(){ - sharedObject = null; - } }