From 9b1abb07fbafaadcb5151dedb1f7b435eff9841e Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Tue, 29 Dec 2020 11:02:52 +0200 Subject: [PATCH 1/3] BAEL-4472- Binary Semaphore vs a ReentrantLock - Unit test added --- ...inarySemaphoreVsReentrantLockUnitTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java new file mode 100644 index 0000000000..f456e82f39 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.binarysemaphorereentrantlock; + +import static org.junit.Assert.assertEquals; +import java.util.concurrent.Semaphore; +import java.util.concurrent.locks.ReentrantLock; + +import org.junit.Test; + +public class BinarySemaphoreVsReentrantLockUnitTest { + + @Test + public void givenBinarySemaphore_whenAcquireAndRelease_thenCheckAvailablePermits() throws InterruptedException { + Semaphore binarySemaphore = new Semaphore(1); + try { + binarySemaphore.acquire(); + assertEquals(0, binarySemaphore.availablePermits()); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + binarySemaphore.release(); + assertEquals(1, binarySemaphore.availablePermits()); + } + } + + @Test + public void givenReentrantLock_whenLockAndUnlock_thenCheckHoldCountAndIsLocked() throws InterruptedException { + ReentrantLock reentrantLock = new ReentrantLock(); + try { + reentrantLock.lock(); + assertEquals(1, reentrantLock.getHoldCount()); + assertEquals(true, reentrantLock.isLocked()); + } finally { + reentrantLock.unlock(); + assertEquals(0, reentrantLock.getHoldCount()); + assertEquals(false, reentrantLock.isLocked()); + } + } + + @Test + public void givenReentrantLock_whenLockMultipleTimes_thenUnlockMultipleTimesToRelease() throws InterruptedException { + ReentrantLock reentrantLock = new ReentrantLock(); + try { + reentrantLock.lock(); + reentrantLock.lock(); + assertEquals(2, reentrantLock.getHoldCount()); + assertEquals(true, reentrantLock.isLocked()); + } finally { + reentrantLock.unlock(); + assertEquals(1, reentrantLock.getHoldCount()); + assertEquals(true, reentrantLock.isLocked()); + + reentrantLock.unlock(); + assertEquals(0, reentrantLock.getHoldCount()); + assertEquals(false, reentrantLock.isLocked()); + } + } + +} From bbdcf57afb63dda1b6e020ba22c5d1a780ef1c92 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Tue, 29 Dec 2020 11:02:52 +0200 Subject: [PATCH 2/3] BAEL-4472- Binary Semaphore vs a ReentrantLock - new module added --- .../core-java-concurrency-advanced-4/pom.xml | 47 +++++++++++++++ ...inarySemaphoreVsReentrantLockUnitTest.java | 58 +++++++++++++++++++ core-java-modules/pom.xml | 1 + 3 files changed, 106 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-advanced-4/pom.xml create mode 100644 core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java diff --git a/core-java-modules/core-java-concurrency-advanced-4/pom.xml b/core-java-modules/core-java-concurrency-advanced-4/pom.xml new file mode 100644 index 0000000000..eb9ed3adc1 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/pom.xml @@ -0,0 +1,47 @@ + + + + 4.0.0 + core-java-concurrency-advanced-4 + 0.1.0-SNAPSHOT + core-java-concurrency-advanced-4 + jar + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + ../ + + + + + + + core-java-concurrency-advanced-4 + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + src/main/resources + true + + + + + + 1.8 + 1.8 + + + diff --git a/core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java b/core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java new file mode 100644 index 0000000000..f456e82f39 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-4/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java @@ -0,0 +1,58 @@ +package com.baeldung.binarysemaphorereentrantlock; + +import static org.junit.Assert.assertEquals; +import java.util.concurrent.Semaphore; +import java.util.concurrent.locks.ReentrantLock; + +import org.junit.Test; + +public class BinarySemaphoreVsReentrantLockUnitTest { + + @Test + public void givenBinarySemaphore_whenAcquireAndRelease_thenCheckAvailablePermits() throws InterruptedException { + Semaphore binarySemaphore = new Semaphore(1); + try { + binarySemaphore.acquire(); + assertEquals(0, binarySemaphore.availablePermits()); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + binarySemaphore.release(); + assertEquals(1, binarySemaphore.availablePermits()); + } + } + + @Test + public void givenReentrantLock_whenLockAndUnlock_thenCheckHoldCountAndIsLocked() throws InterruptedException { + ReentrantLock reentrantLock = new ReentrantLock(); + try { + reentrantLock.lock(); + assertEquals(1, reentrantLock.getHoldCount()); + assertEquals(true, reentrantLock.isLocked()); + } finally { + reentrantLock.unlock(); + assertEquals(0, reentrantLock.getHoldCount()); + assertEquals(false, reentrantLock.isLocked()); + } + } + + @Test + public void givenReentrantLock_whenLockMultipleTimes_thenUnlockMultipleTimesToRelease() throws InterruptedException { + ReentrantLock reentrantLock = new ReentrantLock(); + try { + reentrantLock.lock(); + reentrantLock.lock(); + assertEquals(2, reentrantLock.getHoldCount()); + assertEquals(true, reentrantLock.isLocked()); + } finally { + reentrantLock.unlock(); + assertEquals(1, reentrantLock.getHoldCount()); + assertEquals(true, reentrantLock.isLocked()); + + reentrantLock.unlock(); + assertEquals(0, reentrantLock.getHoldCount()); + assertEquals(false, reentrantLock.isLocked()); + } + } + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 0a9e818156..00ed22c3bd 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -47,6 +47,7 @@ core-java-concurrency-advanced core-java-concurrency-advanced-2 core-java-concurrency-advanced-3 + core-java-concurrency-advanced-4 core-java-concurrency-basic core-java-concurrency-basic-2 core-java-concurrency-collections From adfede63ba31352ca99e0631330a5e9856646096 Mon Sep 17 00:00:00 2001 From: Anshul BANSAL Date: Sat, 9 Jan 2021 21:18:07 +0200 Subject: [PATCH 3/3] BAEL-4472 - old file removed --- ...inarySemaphoreVsReentrantLockUnitTest.java | 58 ------------------- 1 file changed, 58 deletions(-) delete mode 100644 core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java diff --git a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java b/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java deleted file mode 100644 index f456e82f39..0000000000 --- a/core-java-modules/core-java-concurrency-advanced-3/src/test/java/com/baeldung/binarysemaphorereentrantlock/BinarySemaphoreVsReentrantLockUnitTest.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.baeldung.binarysemaphorereentrantlock; - -import static org.junit.Assert.assertEquals; -import java.util.concurrent.Semaphore; -import java.util.concurrent.locks.ReentrantLock; - -import org.junit.Test; - -public class BinarySemaphoreVsReentrantLockUnitTest { - - @Test - public void givenBinarySemaphore_whenAcquireAndRelease_thenCheckAvailablePermits() throws InterruptedException { - Semaphore binarySemaphore = new Semaphore(1); - try { - binarySemaphore.acquire(); - assertEquals(0, binarySemaphore.availablePermits()); - } catch (InterruptedException e) { - e.printStackTrace(); - } finally { - binarySemaphore.release(); - assertEquals(1, binarySemaphore.availablePermits()); - } - } - - @Test - public void givenReentrantLock_whenLockAndUnlock_thenCheckHoldCountAndIsLocked() throws InterruptedException { - ReentrantLock reentrantLock = new ReentrantLock(); - try { - reentrantLock.lock(); - assertEquals(1, reentrantLock.getHoldCount()); - assertEquals(true, reentrantLock.isLocked()); - } finally { - reentrantLock.unlock(); - assertEquals(0, reentrantLock.getHoldCount()); - assertEquals(false, reentrantLock.isLocked()); - } - } - - @Test - public void givenReentrantLock_whenLockMultipleTimes_thenUnlockMultipleTimesToRelease() throws InterruptedException { - ReentrantLock reentrantLock = new ReentrantLock(); - try { - reentrantLock.lock(); - reentrantLock.lock(); - assertEquals(2, reentrantLock.getHoldCount()); - assertEquals(true, reentrantLock.isLocked()); - } finally { - reentrantLock.unlock(); - assertEquals(1, reentrantLock.getHoldCount()); - assertEquals(true, reentrantLock.isLocked()); - - reentrantLock.unlock(); - assertEquals(0, reentrantLock.getHoldCount()); - assertEquals(false, reentrantLock.isLocked()); - } - } - -}