diff --git a/core-java-modules/core-java-concurrency-advanced-5/src/main/java/com/baeldung/countdownlatchvssemaphore/CountDownLatchDemo.java b/core-java-modules/core-java-concurrency-advanced-5/src/main/java/com/baeldung/countdownlatchvssemaphore/CountDownLatchDemo.java new file mode 100644 index 0000000000..e2435bb544 --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-5/src/main/java/com/baeldung/countdownlatchvssemaphore/CountDownLatchDemo.java @@ -0,0 +1,33 @@ +package com.baeldung.countdownlatchvssemaphore; + +import java.util.concurrent.CountDownLatch; + +public class CountDownLatchDemo { + + public static void main(String[] args) throws InterruptedException { + // Create a CountDownLatch with an initial count equal to the number of tasks to be completed + int numberOfTasks = 3; + CountDownLatch latch = new CountDownLatch(numberOfTasks); + + // Simulate completion of tasks by worker threads + for (int i = 1; i <= numberOfTasks; i++) { + new Thread(() -> { + System.out.println("Task completed by Thread " + Thread.currentThread() + .getId()); + + // Decrement the latch count to signal completion of a task + latch.countDown(); + }).start(); + } + + // Main thread waits until all tasks are completed + latch.await(); + System.out.println("All tasks completed. Main thread proceeds."); + + // Attempting to reset will have no effect + latch.countDown(); + // Latch is already at zero, await() returns immediately + latch.await(); // This line won't block + System.out.println("Latch is already at zero and cannot be reset."); + } +} diff --git a/core-java-modules/core-java-concurrency-advanced-5/src/main/java/com/baeldung/countdownlatchvssemaphore/SemaphoreDemo.java b/core-java-modules/core-java-concurrency-advanced-5/src/main/java/com/baeldung/countdownlatchvssemaphore/SemaphoreDemo.java new file mode 100644 index 0000000000..483462e7fb --- /dev/null +++ b/core-java-modules/core-java-concurrency-advanced-5/src/main/java/com/baeldung/countdownlatchvssemaphore/SemaphoreDemo.java @@ -0,0 +1,42 @@ +package com.baeldung.countdownlatchvssemaphore; + +import java.util.concurrent.Semaphore; + +public class SemaphoreDemo { + + public static void main(String[] args) { + // Create a Semaphore with a fixed number of permits + int NUM_PERMITS = 3; + Semaphore semaphore = new Semaphore(NUM_PERMITS); + + // Simulate resource access by worker threads + for (int i = 1; i <= 5; i++) { + new Thread(() -> { + try { + // Acquire a permit to access the resource + semaphore.acquire(); + System.out.println("Thread " + Thread.currentThread().getId() + " accessing resource."); + + // Simulate resource usage + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + // Release the permit after resource access is complete + semaphore.release(); + } + }).start(); + } + + // Simulate resetting the Semaphore by releasing additional permits after a delay + try { + Thread.sleep(5000); + + // Resetting the semaphore permits to the initial count + semaphore.release(NUM_PERMITS); + System.out.println("Semaphore permits reset to initial count."); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file