Semaphore

java.util.concurrent.Semaphore – Semaphore Java Example

Photo of Chandan SinghChandan SinghNovember 17th, 2014Last Updated: March 29th, 2021
0 234 2 minutes read

In this example, we will show you how to make use of the Semaphore –java.util.concurrent.Semaphore Class in Java.

1. What is a SEMAPHORE?

Asemaphore in Computer Science is analogous to a Guard at the entrance of a building. However, this guard takes into account the number of people already entered the building. At any given time, there can be only a fixed amount of people inside the building. When a person leaves the building, the guard allows a new person to enter the building. In the computer science realm, this sort of guard is called aSemaphore. The concept of Semaphore was invented by Edsger Dijkstra in 1965.

Semaphore Java

The semaphores in computer science can be broadly classified as :

  • Binary Semaphore
  • Counting Semaphore

Binary Semaphore has only two states on and off(lock/unlock).(can be implemented in Java by initializing Semaphore to size 1.)
Thejava.util.concurrent.Semaphore Class implements a countingSemaphore.

2. Features of Semaphore Class in Java

  • TheSemaphore Class constructor takes aint parameter, which represents the number of virtual permits the corresponding semaphore object will allow.
  • The semaphore has two main methods that are used to acquire() and release() locks.
  • The Semaphore Class also supports Fairness setting by the boolean fair parameter. However, fairness leads to reduced application throughput and as such should be used sparingly.
  • TheSemaphore Class also supports non-blocking methods liketryacquire() andtryacquire(int permits).

SemaphoreDemo.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
packagecom.javacodegeeks.examples.concurrent;
 
importjava.util.concurrent.Semaphore;
 
/**
 * @author Chandan Singh
 *This class demonstrates the use of java.util.concurrent.Semaphore Class
 */
publicclassSemaphoreDemo
{
     Semaphore semaphore =newSemaphore(10);
         
     publicvoidprintLock()
     {
    try
        {
             semaphore.acquire();
         System.out.println("Locks acquired");
         System.out.println("Locks remaining >> "+semaphore.availablePermits());
        }
    catch(InterruptedException ie)
    {
         ie.printStackTrace();
    }
    finally
    {
         semaphore.release();
         System.out.println("Locks Released");
    }      
     }
      
     publicstaticvoidmain(String[] args)
     {
        finalSemaphoreDemo semaphoreDemo =newSemaphoreDemo();
        Thread thread =newThread(){
        @Override
        publicvoidrun()
        {
               semaphoreDemo.printLock();
        }};
        thread.start();
    }
}

Output:

1
2
3
Locks acquired
Locks remaining >> 9
Locks Released

3. Applications

One of the major application ofSemaphore is while the creation of pooled resources like Database connections.

4. Download the Source Code

We have studied what semaphores are in Java, and how we can use them in our programs.

Download
You can download the source code of this example here:java.util.concurrent.Semaphore – Semaphore Java Example

Last updated on June 01st, 2020

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Chandan SinghChandan SinghNovember 17th, 2014Last Updated: March 29th, 2021
0 234 2 minutes read
Photo of Chandan Singh

Chandan Singh

Chandan holds a degree in Computer Engineering and is a passionate software programmer. He has good experience in Java/J2EE Web-Application development for Banking and E-Commerce Domains.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.