Synchronized Vs Semaphore

Addict picture Addict · Jun 4, 2013 · Viewed 19.3k times · Source

While reading concurrency in Java, I have following doubts:

  1. Does Java provides lower level construct then synchronized for synchronization?

  2. In what circumstances will we use semaphore over synchronized (which provides monitor behaviour in Java)

Answer

Marichyasana picture Marichyasana · Jun 4, 2013

Synchronized allows only one thread of execution to access the resource at the same time. Semaphore allows up to n (you get to choose n) threads of execution to access the resource at the same time.