How to Use Counting Semaphore in Concurrent Java Application? Last Updated : 08 Aug, 2025 Comments Improve Suggest changes Like Article Like Report A Java Counting Semaphore controls access to a shared resource by keeping a set number of permits. A thread must get a permit to use the resource, and if none are available, it waits until another thread releases one. It’s useful for patterns like producer-consumer or managing limited resources such as thread pools or database connections. The java.util.Semaphore class is used for this and is initialized with a set number of permits.Semaphore provides two main methods for obtaining permits and releasing permitsacquire(): 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 a counting semaphore with only one permit, allowing just one thread in a critical section at a time. A thread calls acquire() to enter and release() to exit, ensuring mutual exclusion for essential code.Example: Java import java.util.concurrent.Semaphore; public class SemaphoreTest { // Create a semaphore with a single permit Semaphore binary = new Semaphore(1); public static void main(String args[]) { final SemaphoreTest test = new SemaphoreTest(); // Create and start the first thread new Thread() { @Override public void run() { test.mutualExclusion(); } }.start(); // Create and start the second thread new Thread() { @Override public void run() { test.mutualExclusion(); } }.start(); } private void mutualExclusion() { try { // Try to acquire the semaphore binary.acquire(); // Synchronized block to ensure sequential printing for each thread synchronized (System.out) { // Print a message indicating that the current thread is inside the mutual exclusive region System.out.println(Thread.currentThread().getName() + " inside mutual exclusive "); } // Make the current thread sleep for 1 second Thread.sleep(1000); } catch (InterruptedException e) { // Print the stack trace for the InterruptedException e.printStackTrace(); } finally { // Release the semaphore binary.release(); // Synchronized block to ensure sequential printing for each thread synchronized (System.out) { System.out.println(Thread.currentThread().getName() + " outside of mutual exclusive "); } } } } OutputThread-0 inside mutual exclusive Thread-1 inside mutual exclusive Thread-0 outside of mutual exclusive Thread-1 outside of mutual exclusive Create Quiz Comment K kapilnama1998 Follow 0 Improve K kapilnama1998 Follow 0 Improve Article Tags : Java Java-Multithreading Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like