Open In App

How to Use Counting Semaphore in Concurrent Java Application?

Improve
Improve
Like Article
Like
Save
Share
Report

Java Counting Semaphore maintains a specified number of passes or permissions, and the Current Thread must obtain a permit to access a shared resource. If a permit is already exhausted by threads other than that, it may wait until the permit becomes available as a result of the release of permits from various threads. This concurrency utility can be very useful for implementing a pattern of producer-consumer design or implementing limited pools of assets such as Thread Pool, DB Connection Pool, etc. The class java.util.Semaphore is a Counting Semaphore that is initialized with a number of permissions. 

How to Use Counting Semaphore in Concurrent Java Application?

Semaphore provides two main methods for obtaining permits and releasing permits

  • acquire(): This method acquires a permit if one is available, and returns immediately, reducing the number of available permits by one. If the current thread is interrupted while waiting for a permit then InterruptedException is thrown.
  • release(): This method acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount. If the current thread is interrupted while waiting for a permit then InterruptedException is thrown.

Implementation:

A binary semaphore is known as a Counting semaphore with one permit because it only has two state permits available or unavailable permits. To execute mutual exclusion or critical section where only one thread is allowed to execute, a binary semaphore can be used. A thread waits on acquire() until Thread allows release within the critical section by calling release() on the semaphore. Below is java semaphore counting where binary semaphore is used to provide shared exclusive access to essential code parts

Example:

Java




// Java Program to illustrate use Counting Semaphore
// in Concurrent Java Application
 
import java.util.concurrent.Semaphore;
 
public class SemaphoreTest {
 
    // Initialize the semaphore with the number
    // of permits required
 
    // Here only 1 permit is allowed
    Semaphore binary = new Semaphore(1);
 
    // Main driver method
    public static void main(String args[])
    {
 
        final SemaphoreTest test = new SemaphoreTest();
 
        // Thread 1
        new Thread() {
           
            // Method that should be executed for thread1
            @Override public void run()
            {
                test.mutualExclusion();
            }
        }.start();
 
        // Thread 2
        new Thread() {
           
            // Method that should be executed for thread2
            @Override public void run()
            {
                test.mutualExclusion();
            }
        }.start();
    }
 
    // Method
    private void mutualExclusion()
    {
 
        // Try block to check for exceptions
        try {
            // acquire() acts as an input to semaphore
            // to check for available permits
            binary.acquire();
 
            // Mutual exclusive region
 
            System.out.println(
                Thread.currentThread().getName()
                + " inside mutual exclusive ");
 
            // sleep() method is used to hold thread for
            // sometime Parameter is nanoseconds to be
            // holded
            Thread.sleep(1000);
        }
 
        // Catch block to handle the exception
 
        // Handling exception if thread is interrupted
        // either before or during the activity
        catch (InterruptedException e) {
 
            // Print and display the line number where
            // exception occurred
            e.printStackTrace();
        }
 
        finally {
 
            // release() method acts as output to semaphore
            // When thread operation is completed
            // release() method increase the permits
            // in the semaphore
            binary.release();
 
            // Printing the thread name
            // using the getName() method
            System.out.println(
                Thread.currentThread().getName()
                + " outside of mutual exclusive ");
        }
    }
}


Output

Thread-0 inside mutual exclusive 
Thread-1 inside mutual exclusive 
Thread-0 outside of mutual exclusive 
Thread-1 outside of mutual exclusive 


Last Updated : 08 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads