Monday, September 30, 2013

Semaphore vs CountDownLatch in java


I recommend  before looking for a difference between semaphore and CountDownLatch  if you go through the following post, difference is easy to understand as semaphore and CountDownLatch are explained with detailed examples.


CountDownLatch with examples
Semaphore with examples



Both CountDownLatch and semaphore used for difference scenario and main difference is


  • CountdownLatch is used to start a series of threads and then wait until all of them are complete a given number of times, for instance, before starting main service you want to start initializing services like db connection, starting logging service etc.
  • Semaphore is used to control the number of concurrent threads that are using a resource.That resource can be something like a shared data, or any file. The count on a Semaphore can go up and down as different threads call acquire() and release().






Related Post
Daemon Thread in java with demo example
Concept of Upcasting and downcasting in java with example
Why java doesn't support operator overloading
when to use concurrent Has Map



Thanks for reading the post !
Please share or comment on this as this encourage me to write more :) 



No comments:

Post a Comment