BAEL-7430 first draft (#15728)
* BAEL-7430 first draft * Revised the code --------- Co-authored-by: Wynn Teo <wynnteo@Wynns-MacBook-Pro.local>
This commit is contained in:
		
							parent
							
								
									b490028e69
								
							
						
					
					
						commit
						fd59ccd374
					
				| @ -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."); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -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(); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user