Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Semaphore in Java
Next article icon

Semaphore in Java

Last Updated : 11 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource. Thus, to access the resource, a thread must be granted a permit from the semaphore.

Working of semaphore

In general, to use a semaphore, the thread that wants access to the shared resource tries to acquire a permit.
  • If the semaphore’s count is greater than zero, then the thread acquires a permit, which causes the semaphore’s count to be decremented.
  • Otherwise, the thread will be blocked until a permit can be acquired.
  • When the thread no longer needs an access to the shared resource, it releases the permit, which causes the semaphore’s count to be incremented.
  • If there is another thread waiting for a permit, then that thread will acquire a permit at that time.
Java provide Semaphore class in java.util.concurrent package that implements this mechanism, so you don't have to implement your own semaphores. Flow Diagram : d Constructors in Semaphore class : There are two constructors in Semaphore class.
  Semaphore(int num)  Semaphore(int num, boolean how)  
Here, num specifies the initial permit count. Thus, it specifies the number of threads that can access a shared resource at any one time. If it is one, then only one thread can access the resource at any one time. By default, all waiting threads are granted a permit in an undefined order. By setting how to true, you can ensure that waiting threads are granted a permit in the order in which they requested access.

Using Semaphores as Locks(preventing race condition)

We can use a semaphore to lock access to a resource, each thread that wants to use that resource must first call acquire( ) before accessing the resource to acquire the lock. When the thread is done with the resource, it must call release( ) to release lock. Here is an example that demonstrate this: Java
// java program to demonstrate  // use of semaphores Locks import java.util.concurrent.*;  //A shared resource/class. class Shared  {     static int count = 0; }  class MyThread extends Thread {     Semaphore sem;     String threadName;     public MyThread(Semaphore sem, String threadName)      {         super(threadName);         this.sem = sem;         this.threadName = threadName;     }      @Override     public void run() {                  // run by thread A         if(this.getName().equals("A"))         {             System.out.println("Starting " + threadName);             try              {                 // First, get a permit.                 System.out.println(threadName + " is waiting for a permit.");                              // acquiring the lock                 sem.acquire();                              System.out.println(threadName + " gets a permit.");                          // Now, accessing the shared resource.                 // other waiting threads will wait, until this                  // thread release the lock                 for(int i=0; i < 5; i++)                 {                     Shared.count++;                     System.out.println(threadName + ": " + Shared.count);                              // Now, allowing a context switch -- if possible.                     // for thread B to execute                     Thread.sleep(10);                 }             } catch (InterruptedException exc) {                     System.out.println(exc);                 }                          // Release the permit.                 System.out.println(threadName + " releases the permit.");                 sem.release();         }                  // run by thread B         else         {             System.out.println("Starting " + threadName);             try              {                 // First, get a permit.                 System.out.println(threadName + " is waiting for a permit.");                              // acquiring the lock                 sem.acquire();                              System.out.println(threadName + " gets a permit.");                          // Now, accessing the shared resource.                 // other waiting threads will wait, until this                  // thread release the lock                 for(int i=0; i < 5; i++)                 {                     Shared.count--;                     System.out.println(threadName + ": " + Shared.count);                              // Now, allowing a context switch -- if possible.                     // for thread A to execute                     Thread.sleep(10);                 }             } catch (InterruptedException exc) {                     System.out.println(exc);                 }                 // Release the permit.                 System.out.println(threadName + " releases the permit.");                 sem.release();         }     } }  // Driver class public class SemaphoreDemo  {     public static void main(String args[]) throws InterruptedException      {         // creating a Semaphore object         // with number of permits 1         Semaphore sem = new Semaphore(1);                  // creating two threads with name A and B         // Note that thread A will increment the count         // and thread B will decrement the count         MyThread mt1 = new MyThread(sem, "A");         MyThread mt2 = new MyThread(sem, "B");                  // stating threads A and B         mt1.start();         mt2.start();                  // waiting for threads A and B          mt1.join();         mt2.join();                  // count will always remain 0 after         // both threads will complete their execution         System.out.println("count: " + Shared.count);     } } 
Output:
  Starting A  Starting B  A is waiting for a permit.  B is waiting for a permit.  A gets a permit.  A: 1  A: 2  A: 3  A: 4  A: 5  A releases the permit.  B gets a permit.  B: 4  B: 3  B: 2  B: 1  B: 0  B releases the permit.  count: 0  
Note : The output can be different in different executions of above program, but final value of count variable will always remain 0. Explanation of above program :
  • The program uses a semaphore to control access to the count variable, which is a static variable within the Shared class. Shared.count is incremented five times by thread A and decremented five times by thread B.To prevent these two threads from accessing Shared.count at the same time, access is allowed only after a permit is acquired from the controlling semaphore. After access is complete, the permit is released. In this way, only one thread at a time will access Shared.count, as the output shows.
  • Notice the call to sleep( ) within run( ) method inside MyThread class. It is used to “prove” that accesses to Shared.count are synchronized by the semaphore. In run( ), the call to sleep( ) causes the invoking thread to pause between each access to Shared.count. This would normally enable the second thread to run. However, because of the semaphore, the second thread must wait until the first has released the permit, which happens only after all accesses by the first thread are complete. Thus, Shared.count is first incremented five times by thread A and then decremented five times by thread B. The increments and decrements are not intermixed at assembly code.
  • Without the use of the semaphore, accesses to Shared.count by both threads would have occurred simultaneously, and the increments and decrements would be intermixed.To confirm this, try commenting out the calls to acquire( ) and release( ). When you run the program, you will see that access to Shared.count is no longer synchronized, thus you will not always get count value 0.
Next: Java.util.concurrent.Semaphore class in Java

Next Article
Semaphore in Java

G

gaurav miglani
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

    Multithreading in Java
    Multithreading is a Java feature that allows the concurrent execution of two or more parts of a program for maximum utilization of the CPU. Each part of such a program is called a thread. So, threads are lightweight processes within a process.Different Ways to Create ThreadsThreads can be created by
    3 min read
    Lifecycle and States of a Thread in Java
    A thread in Java can exist in any one of the following states at any given time. A thread lies only in one of the shown states at any instant:New StateRunnable StateBlocked StateWaiting StateTimed Waiting StateTerminated StateThe diagram below represents various states of a thread at any instant:Lif
    5 min read
    Main thread in Java
    Java provides built-in support for multithreaded programming. A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.When a Java program starts up, one thread begins running i
    4 min read
    Java Concurrency - yield(), sleep() and join() Methods
    In this article, we will learn what is yield(), join(), and sleep() methods in Java and what is the basic difference between these three. First, we will see the basic introduction of all these three methods, and then we compare these three. We can prevent the execution of a thread by using one of th
    5 min read
    Inter-thread Communication in Java
    Inter-thread communication in Java is a mechanism in which a thread is paused from running in its critical section, and another thread is allowed to enter (or lock) the same critical section to be executed.Note: Inter-thread communication is also known as Cooperation in Java.What is Polling, and Wha
    6 min read
    Java Thread Class
    Thread is a line of execution within a program. Each program can have multiple associated threads. Each thread has a priority which is used by the thread scheduler to determine which thread must run first. Java provides a thread class that has various method calls to manage the behavior of threads b
    5 min read
    What does start() function do in multithreading in Java?
    We have discussed that Java threads are typically created using one of the two methods : (1) Extending thread class. (2) Implementing RunnableIn both the approaches, we override the run() function, but we start a thread by calling the start() function. So why don't we directly call the overridden ru
    2 min read
    Java Thread Priority in Multithreading
    Java being Object-Oriented works within a Multithreading environment in which the thread scheduler assigns the processor to a thread based on the priority of the thread. Whenever we create a thread in Java, it always has some priority assigned to it. Priority can either be given by JVM while creatin
    5 min read
    Joining Threads in Java
    java.lang.Thread class provides the join() method which allows one thread to wait until another thread completes its execution. If t is a Thread object whose thread is currently executing, then t.join() will make sure that t is terminated before the next instruction is executed by the program. If th
    3 min read
    Java Naming a Thread and Fetching Name of Current Thread
    A thread can be referred to as a lightweight process. Assigning descriptive names to threads enhances code readability and simplifies debugging. Now let us discuss the different ways to name a thread in Java.Methods to Set the Thread NameThere are two ways by which we can set the name either be it d
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences