Semaphore in Java Concurrency

Semaphore is a synchronization aid that can be used for inter communication among threads or to restrict access to resource. From Java 5 an implementation of semaphore is available for out of box use and it is part of java.util.concurrent package.

Counting semaphore

The implementation of semaphore readily available in the Java concurrency package is a counting semaphore.

Conceptually, a semaphore maintains a set of permits. When a semaphore is created it is created with a given number of permits. The working of semaphore in Java can be explained using the following steps-

  1. A thread that wants to access a shared resource tries to acquire a permit using the acquire() method.
  2. If permit is available or in other words if semaphore count is greater than zero then the thread acquires a permit otherwise thread is blocked.
  3. With every successful acquisition of the permit the count is also decremented. If count becomes zero then no permit can be given.
  4. When the thread is done with the shared resource it can release the acquired permit using the release() method. This increments the semaphore’s count.
  5. Any blocking thread waiting to acquire a permit can get a permit once count is more than zero.

Java semaphore constructors

  • Semaphore(int permits)– Creates a Semaphore with the given number of permits and nonfair fairness setting.
  • Semaphore(int permits, boolean fair)– Creates a Semaphore with the given number of permits and the given fairness setting.

Semaphore example in Java

Let’s say there is a method that is computation heavy and you want to restrict access to this method to 2 threads at any given time. In this scenario you can use Semaphore created with 2 permits.

Output

As you can see at any given time permits are acquired by 2 threads.

Binary Semaphore

A semaphore in Java created with only one permit can serve as a mutual exclusion lock. This is more commonly known as a binary semaphore, because it only has two states: one permit available, or zero permits available.

Binary Semaphore example in Java

Here is a simple binary semaphore example where a shared counter is used among multiple threads. Binary semaphore let only one thread access the shared resource at any given time.

Output

To see how threads can interfere you can comment the acquire and release methods with in the run() method.

Running after commenting gives the following output for one run where same count is displayed for 2 threads.

Methods in Java Semaphore class

Some of the important methods in Semaphore class in Java are as follows-

  1. acquire()– Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.
  2. acquire(int permits)– Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted.
  3. availablePermits()– Returns the current number of permits available in this semaphore.
  4. drainPermits()– Acquires and returns all permits that are immediately available, or if negative permits are available, releases them.
  5. getQueuedThreads()– Returns a collection containing threads that may be waiting to acquire.
  6. isFair()– Returns true if this semaphore has fairness set true.
  7. release()– Releases a permit, returning it to the semaphore.
  8. tryAcquire()– Acquires a permit from this semaphore, only if one is available at the time of invocation.
  9. tryAcquire(int permits)– Acquires the given number of permits from this semaphore, only if all are available at the time of invocation.

That’s all for the topic Semaphore in Java Concurrency. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.