diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java b/core-java/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java index 8ef9cc6ea6..50cd2966ea 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java +++ b/core-java/src/main/java/com/baeldung/concurrent/semaphores/CounterUsingMutex.java @@ -2,17 +2,17 @@ package com.baeldung.concurrent.semaphores; import java.util.concurrent.Semaphore; -public class CounterUsingMutex { +class CounterUsingMutex { private final Semaphore mutex; private int count; - public CounterUsingMutex() { + CounterUsingMutex() { mutex = new Semaphore(1); count = 0; } - public void increase() throws InterruptedException { + void increase() throws InterruptedException { mutex.acquire(); this.count = this.count + 1; Thread.sleep(1000); @@ -20,11 +20,11 @@ public class CounterUsingMutex { } - public int getCount() { + int getCount() { return this.count; } - public boolean hasQueuedThreads() { + boolean hasQueuedThreads() { return mutex.hasQueuedThreads(); } diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java b/core-java/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java index a27b490fde..718574d3c8 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java +++ b/core-java/src/main/java/com/baeldung/concurrent/semaphores/DelayQueueUsingTimedSemaphore.java @@ -4,19 +4,19 @@ import java.util.concurrent.TimeUnit; import org.apache.commons.lang3.concurrent.TimedSemaphore; -public class DelayQueueUsingTimedSemaphore { +class DelayQueueUsingTimedSemaphore { private final TimedSemaphore semaphore; - public DelayQueueUsingTimedSemaphore(long period, int slotLimit) { + DelayQueueUsingTimedSemaphore(long period, int slotLimit) { semaphore = new TimedSemaphore(period, TimeUnit.SECONDS, slotLimit); } - public boolean tryAdd() { + boolean tryAdd() { return semaphore.tryAcquire(); } - public int availableSlots() { + int availableSlots() { return semaphore.getAvailablePermits(); } diff --git a/core-java/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java b/core-java/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java index 38dbc53ad3..3cf7c45428 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java +++ b/core-java/src/main/java/com/baeldung/concurrent/semaphores/LoginQueueUsingSemaphore.java @@ -2,23 +2,23 @@ package com.baeldung.concurrent.semaphores; import java.util.concurrent.Semaphore; -public class LoginQueueUsingSemaphore { +class LoginQueueUsingSemaphore { private final Semaphore semaphore; - public LoginQueueUsingSemaphore(int slotLimit) { + LoginQueueUsingSemaphore(int slotLimit) { semaphore = new Semaphore(slotLimit); } - public boolean tryLogin() { + boolean tryLogin() { return semaphore.tryAcquire(); } - public void logout() { + void logout() { semaphore.release(); } - public int availableSlots() { + int availableSlots() { return semaphore.availablePermits(); } diff --git a/core-java/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java b/core-java/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java index 9686bf6444..8d64bb6809 100644 --- a/core-java/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java +++ b/core-java/src/test/java/com/baeldung/concurrent/semaphores/SemaphoresManualTest.java @@ -1,14 +1,14 @@ package com.baeldung.concurrent.semaphores; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import org.junit.Test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.stream.IntStream; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class SemaphoresManualTest { @@ -20,12 +20,7 @@ public class SemaphoresManualTest { final ExecutorService executorService = Executors.newFixedThreadPool(slots); final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots); IntStream.range(0, slots) - .forEach(user -> executorService.execute(new Runnable() { - @Override - public void run() { - loginQueue.tryLogin(); - } - })); + .forEach(user -> executorService.execute(loginQueue::tryLogin)); executorService.shutdown(); assertEquals(0, loginQueue.availableSlots()); @@ -38,12 +33,7 @@ public class SemaphoresManualTest { final ExecutorService executorService = Executors.newFixedThreadPool(slots); final LoginQueueUsingSemaphore loginQueue = new LoginQueueUsingSemaphore(slots); IntStream.range(0, slots) - .forEach(user -> executorService.execute(new Runnable() { - @Override - public void run() { - loginQueue.tryLogin(); - } - })); + .forEach(user -> executorService.execute(loginQueue::tryLogin)); executorService.shutdown(); assertEquals(0, loginQueue.availableSlots()); @@ -60,12 +50,7 @@ public class SemaphoresManualTest { final ExecutorService executorService = Executors.newFixedThreadPool(slots); final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots); IntStream.range(0, slots) - .forEach(user -> executorService.execute(new Runnable() { - @Override - public void run() { - delayQueue.tryAdd(); - } - })); + .forEach(user -> executorService.execute(delayQueue::tryAdd)); executorService.shutdown(); assertEquals(0, delayQueue.availableSlots()); @@ -78,12 +63,7 @@ public class SemaphoresManualTest { final ExecutorService executorService = Executors.newFixedThreadPool(slots); final DelayQueueUsingTimedSemaphore delayQueue = new DelayQueueUsingTimedSemaphore(1, slots); IntStream.range(0, slots) - .forEach(user -> executorService.execute(new Runnable() { - @Override - public void run() { - delayQueue.tryAdd(); - } - })); + .forEach(user -> executorService.execute(delayQueue::tryAdd)); executorService.shutdown(); assertEquals(0, delayQueue.availableSlots()); @@ -100,16 +80,13 @@ public class SemaphoresManualTest { final ExecutorService executorService = Executors.newFixedThreadPool(count); final CounterUsingMutex counter = new CounterUsingMutex(); IntStream.range(0, count) - .forEach(user -> executorService.execute(new Runnable() { - @Override - public void run() { - try { - counter.increase(); - } catch (final InterruptedException e) { - e.printStackTrace(); - } - } - })); + .forEach(user -> executorService.execute(() -> { + try { + counter.increase(); + } catch (final InterruptedException e) { + e.printStackTrace(); + } + })); executorService.shutdown(); assertTrue(counter.hasQueuedThreads()); @@ -121,16 +98,13 @@ public class SemaphoresManualTest { final ExecutorService executorService = Executors.newFixedThreadPool(count); final CounterUsingMutex counter = new CounterUsingMutex(); IntStream.range(0, count) - .forEach(user -> executorService.execute(new Runnable() { - @Override - public void run() { - try { - counter.increase(); - } catch (final InterruptedException e) { - e.printStackTrace(); - } - } - })); + .forEach(user -> executorService.execute(() -> { + try { + counter.increase(); + } catch (final InterruptedException e) { + e.printStackTrace(); + } + })); executorService.shutdown(); assertTrue(counter.hasQueuedThreads()); Thread.sleep(5000);