SlideShare a Scribd company logo
1 of 14
Download to read offline
Semaphore in Java with Example
In Java, we usually implement semaphores in thread synchronization. This article
will give you a clear insight into this concept. Let’s start!!!
What is semaphore in Java?
When there are many threads waiting to access a shared resource, various issues
occur in resource allocation. A semaphore makes this task a bit easier by limiting the
number of threads that can access a shared resource. It is a non-negative variable
that is shared among the thread is denoted as a counter.
■ If counter > 0, access to the resources is granted.
■ If counter = 0, access to the resources is denied.
It sends signals among the threads to maintain cooperation during this process. It
helps in avoiding race conditions and synchronizes the processes effectively.
To put it in simple words, a semaphore can limit the maximum of 5 connections to
access a file concurrently.
Features of Java semaphore:
■ It allows synchronization to take place among the threads.
■ It provides a low-level synchronization mechanism by decreasing the
level of synchronization.
■ The value of a semaphore is either zero or greater than zero. It is never a
negative value.
■ The implementation of a semaphore can be done using test operations
and interrupts. To execute a semaphore, file descriptors are used.
How does a semaphore works in java?
It takes control over the shared resources and regulates them using a counter
variable. We already knew that this counter variable could not be a negative value. It
can be zero or greater than 0.
If the counter > 0, the thread will be granted permission to access the shared
resource. In this situation, the counter value will be decremented by 1.
Else, the thread stays in the blocked state until it gains permission.
Once the thread uses the resource, it releases it. At this instance, the counter value
gets incremented by 1.
The next thread that is waiting to access the resource will be granted permission.
When the counter = 0, the thread is denied of being granted permission to access
the shared resource.
Semaphore class in Java:
Java contains easy ways to implement the semaphore mechanisms. This is possible
using the Semaphore class in it. This class is present in java.util.concurrent
package.
As this class provides all the functionalities by default, you need not crack your
heads thinking about what to implement there.
Constructors in Java Semaphore class:
The Semaphore class in Java contains two contractors:
1. Semaphore(int num)
2. Semaphore(int num, boolean how)
The value of num is the number of threads that have permission to access the
shared resource. If the boolean value is set to true, the thread receives access to
use the resource.
Methods present in Java Semaphore
class:
The Semaphore class contains various built-in methods.
The popular methods in this class are:
■ acquire(): Once the permit is available, it returns true; otherwise, it returns
false.
■ release(): It releases the permit.
■ available(): This method returns the count of available permits.
Types of Java Semaphores:
Semaphores are categorized into four types:
1. Counting Semaphores
2. Bounded Semaphores
3. Timed Semaphores
4. Binary Semaphores
1. Counting Semaphores in Java:
In situations where we require more than one process to execute simultaneously, the
counting semaphores play a major role. It uses the take() method to achieve this.
Sample program for Counting Semaphore:
public class CoutingSemaphore{
private int signals = 0;
public synchronized void take(){
this.signals++;
this.notify();
}
public synchronized void release() throws InterruptedException{
while(this.signals == 0)
wait();
this.signals--;
}
}
2. Java Bounded semaphores:
There is no upper bound value stored in the counting semaphore. To know the count
of signals it can store, we use the bounded semaphores to set the upper bound.
Sample Program for Bounded Semaphore:
public class BoundedSemaphore{
private int signal = 0;
private int bound = 0;
public BoundedSempahore(int upperBound){
this.bound = upperBound;
}
public void synchronized take() throws InterruptedException{
while(this.signal == bound)
wait();
this.signal++;
this.notify++;
}
public void synchronized release() throws InterruptedException{
while(this.signal == 0)
wait();
this.signal--;
}
}
3. Java Timed Semaphores:
While using this semaphore, we can make the thread run for a certain period of time.
Once the duration gets over, the times reset, and permits get released.
4. Java Binary Semaphore:
The binary semaphores act like the counting semaphores. The only difference is that
binary semaphores accept only binary values, either 0 or 1.
If the binary semaphore value = 1, the operation succeeds. If the value is 0, the
operation does not take place.
Semaphores as Locks in Java:
In Java, using bounded semaphore as a lock can also take place. To achieve this,
we must set the value of the upper bound to 1 and invoke the take() and release()
methods. Using these methods, take control of the critical section.
Sample snippet to implement using semaphores as locks in java:
Ssemaphore sem = new Semaphore(1);
sem.take();
try{
//critical section
}
finally{
sem.release();
}
Implementing Semaphore in Java:
import java.util.concurrent.*;
class FirstCode{
static int count = 0;
}
class FirstCode2 extends Thread{
Semaphore s;
String threadName;
public FirstCode2(Semaphore s, String threadName){
super(threadName);
this.s = s;
this.threadName = threadName;
}@Override
public void run(){
if(this.getName().equals(“Thread1”)){
try{
System.out.println(“Starting ”+ threadName);
try{
System.out.println(threadName + “is waiting for permit”);
s.acquire();
System.out.println(threadName + “gets a permit”);
for(int i = 0; i<5; i++){
SharedResource.count++;
System.out.println(threadName+ “:” + SharedResource.count);
Thread.sleep(10);
}
}
catch(InterruptedExecution exc){
System.out.println(exc);
}
System.out.println(threadName + “releases the permit”);
s.release();
}
else{
System.out.println(“Starting” +threadName);
try{
System.out.println(threadName + “gets a permit”);
for(int i=0; i<5; i++){
SharedResource.count--;
System.out.println(threadName+ “:” + SharedResource.count);
Thread.sleep(10);
}
}
catch(InterruptedException exc){
System.out.println(exc);
}
System.out.println(threadName + “releases the permit”);
s.release();
}
}
}
public class SemaphorePgm{
public static void main(String args[]) throwsInterruptedException{
Semaphore s = new Semaphore(1);
FirstCode2 fc1 = new FirstCode2(s, “Thread1”);
FirstCode2 fc2 = new FirstCode2(s, “Thread2”);
fc1.start();
fc2.start();
System.out.println(“count: ”+ SharedResource.count);
}
}
Output:
Starting Thread1
Starting Thread2
Thread1 is waiting for a permit.
Thread2 is waiting for a permit.
Thread1 gets a permit.
Thread1: 1
Thread1: 2
Thread1: 3
Thread1: 4
Thread1: 5
Thread1 releases the permit.
Thread2 gets a permit.
Thread2: 4
Thread2: 3
Thread2: 2
Thread2: 1
Thread2: 0
Thread2 releases the permit.
count: 0
Explanation of the program:
This program takes the help of a semaphore to control the access of the count
variable, which is a static variable within the Shared class. The Shared.count
increments five times by thread fc1 and decreases five times by thread fc2. Now we
must prevent these two threads from accessing the Shared.count. to achieve this,
access is allowed only after gaining the permit from the controlling semaphore. In
this way, only one thread at a time will access the Shared.count as provided in the
output.
Note that the call to sleep() inside the run() method is within the FirstCode2 class. it
proves the access to Shared.count is synchronized by the semaphore. In the run()
method, the class to sleep() method enables the thread to pause between each
access to Shared.count. Generally, this enables the second thread to run.
Yet, due to the presence of semaphore, this second thread must wait until the first
thread releases its permit. This takes place only if the first thread completes all its
tasks. Therefore, the Shared.count is incremented five times by thread fc1 and
decremented fives times by thread fc2. these increments and decrements are not
intermixed at the assembly code.
In the absence of semaphore, the accesses to Shared.count by both threads would
have occurred simultaneously. Also, the increments and decrements would have
been intermixed. To validate this, you will notice that the access to Shared.count is
no longer synchronized. Therefore, you will not get the count value to be 0 always.
Using Semaphores as locks to prevent
race conditions:
The semaphore helps us in locking access to a resource. Any string that wants to
utilize the resource should call the obtain() method first. This should take place
before it gets to the asset to gain the bolt. Once the string finishes with the asset, it
must call the discharge() method to discharge the bolt.
Sample program to implement using semaphores as lock to prevent race condition:
class FirstCode{
static int count = 0;
}
class FirstCode2 extends Thread{
Semaphore s;
String threadName;
public FirstCode2(Semaphore s, String threadName){
super(threadName);
this.s = s;
this.threadName = threadName;
}
@Override
public void run(){
if(this.getName().equal(“A”)){
System.out.println(“Starting ”+threadName);
try{
System.out.println(threadName + “is waiting for a permit.”);
s.acquire();
System.out.println(“threaName ”+ “gets a permit.”);
for(int i=0; i<5; i++){
Shared.count++;
Sstem.out.println(threadName + “:” + Shared.count);
Thread.sleep(10);
}
}catch(UnterruptedException exc){
System.out.println(exc);
}
System.out.println(threadName + “releases the permit.”);
s.release();
}
else{
System.out.println(“Starting” + threadName);
try{
System.out.println(threadName + “is waiting for a permit.”);
s.acquire();
System.out.println(threadName+ “gets a permit.”);
for(int i=0; i<5;i++){
Shared.count--;
System.out.println(threadName+ “:” +Shared.count);
Thread.sleep(10);
}
}catch(InterruptedException exc){
System.out.println(exc);
}
System.out.println(threadName+ “releases the permit.”);
s.release();
}
}
}
public class SemaphoreExample{
public static void main(String args[]) throws InterruptedException{
Semaphore s = newSemaphore(1);
FirstCode2 fc = new FirstCode2(s, “A”);
FirstCode2 fc2 = new FirstCode2(s, “B”);
fc.start();
fc2.start();
fc.join();
fc2.join();
System.out.println(“count: ”+Shared.count);
}
}
Output:
Starting A
Starting B
B is waiting for a permit.
B gets a permit.
A is waiting for a permit.
B: -1
B: -2
B: -3
B: -4
B: -5
B releases the permit.
A gets a permit.
A: -4
A: -3
A: -2
A: -1
A: 0
A releases the permit.
count: 0
Summary
This is all about semaphore in java. Hope you enjoyed the article.

More Related Content

Similar to Semaphore in Java with Example.pdf

CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual Project
ThienSi Le
 

Similar to Semaphore in Java with Example.pdf (20)

MultiThreading in Python
MultiThreading in PythonMultiThreading in Python
MultiThreading in Python
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Shared memory and semaphore? And how to use them? An explanation about those ...
Shared memory and semaphore? And how to use them? An explanation about those ...Shared memory and semaphore? And how to use them? An explanation about those ...
Shared memory and semaphore? And how to use them? An explanation about those ...
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java
JavaJava
Java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
multithreading
multithreadingmultithreading
multithreading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Java
JavaJava
Java
 
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual Project
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 

More from SudhanshiBakre1

More from SudhanshiBakre1 (20)

IoT Security.pdf
IoT Security.pdfIoT Security.pdf
IoT Security.pdf
 
Top Java Frameworks.pdf
Top Java Frameworks.pdfTop Java Frameworks.pdf
Top Java Frameworks.pdf
 
Numpy ndarrays.pdf
Numpy ndarrays.pdfNumpy ndarrays.pdf
Numpy ndarrays.pdf
 
Float Data Type in C.pdf
Float Data Type in C.pdfFloat Data Type in C.pdf
Float Data Type in C.pdf
 
IoT Hardware – The Backbone of Smart Devices.pdf
IoT Hardware – The Backbone of Smart Devices.pdfIoT Hardware – The Backbone of Smart Devices.pdf
IoT Hardware – The Backbone of Smart Devices.pdf
 
Internet of Things – Contiki.pdf
Internet of Things – Contiki.pdfInternet of Things – Contiki.pdf
Internet of Things – Contiki.pdf
 
Java abstract Keyword.pdf
Java abstract Keyword.pdfJava abstract Keyword.pdf
Java abstract Keyword.pdf
 
Node.js with MySQL.pdf
Node.js with MySQL.pdfNode.js with MySQL.pdf
Node.js with MySQL.pdf
 
Collections in Python - Where Data Finds Its Perfect Home.pdf
Collections in Python - Where Data Finds Its Perfect Home.pdfCollections in Python - Where Data Finds Its Perfect Home.pdf
Collections in Python - Where Data Finds Its Perfect Home.pdf
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Types of AI you should know.pdf
Types of AI you should know.pdfTypes of AI you should know.pdf
Types of AI you should know.pdf
 
Streams in Node .pdf
Streams in Node .pdfStreams in Node .pdf
Streams in Node .pdf
 
Annotations in Java with Example.pdf
Annotations in Java with Example.pdfAnnotations in Java with Example.pdf
Annotations in Java with Example.pdf
 
RESTful API in Node.pdf
RESTful API in Node.pdfRESTful API in Node.pdf
RESTful API in Node.pdf
 
Top Cryptocurrency Exchanges of 2023.pdf
Top Cryptocurrency Exchanges of 2023.pdfTop Cryptocurrency Exchanges of 2023.pdf
Top Cryptocurrency Exchanges of 2023.pdf
 
Epic Python Face-Off -Methods vs.pdf
Epic Python Face-Off -Methods vs.pdfEpic Python Face-Off -Methods vs.pdf
Epic Python Face-Off -Methods vs.pdf
 
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdfDjango Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
Django Tutorial_ Let’s take a deep dive into Django’s web framework.pdf
 
Benefits Of IoT Salesforce.pdf
Benefits Of IoT Salesforce.pdfBenefits Of IoT Salesforce.pdf
Benefits Of IoT Salesforce.pdf
 
Epic Python Face-Off -Methods vs. Functions.pdf
Epic Python Face-Off -Methods vs. Functions.pdfEpic Python Face-Off -Methods vs. Functions.pdf
Epic Python Face-Off -Methods vs. Functions.pdf
 
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdfPython Classes_ Empowering Developers, Enabling Breakthroughs.pdf
Python Classes_ Empowering Developers, Enabling Breakthroughs.pdf
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 

Semaphore in Java with Example.pdf

  • 1. Semaphore in Java with Example In Java, we usually implement semaphores in thread synchronization. This article will give you a clear insight into this concept. Let’s start!!! What is semaphore in Java? When there are many threads waiting to access a shared resource, various issues occur in resource allocation. A semaphore makes this task a bit easier by limiting the number of threads that can access a shared resource. It is a non-negative variable that is shared among the thread is denoted as a counter. ■ If counter > 0, access to the resources is granted. ■ If counter = 0, access to the resources is denied. It sends signals among the threads to maintain cooperation during this process. It helps in avoiding race conditions and synchronizes the processes effectively. To put it in simple words, a semaphore can limit the maximum of 5 connections to access a file concurrently. Features of Java semaphore: ■ It allows synchronization to take place among the threads. ■ It provides a low-level synchronization mechanism by decreasing the level of synchronization. ■ The value of a semaphore is either zero or greater than zero. It is never a negative value. ■ The implementation of a semaphore can be done using test operations and interrupts. To execute a semaphore, file descriptors are used. How does a semaphore works in java?
  • 2. It takes control over the shared resources and regulates them using a counter variable. We already knew that this counter variable could not be a negative value. It can be zero or greater than 0. If the counter > 0, the thread will be granted permission to access the shared resource. In this situation, the counter value will be decremented by 1. Else, the thread stays in the blocked state until it gains permission. Once the thread uses the resource, it releases it. At this instance, the counter value gets incremented by 1. The next thread that is waiting to access the resource will be granted permission. When the counter = 0, the thread is denied of being granted permission to access the shared resource. Semaphore class in Java: Java contains easy ways to implement the semaphore mechanisms. This is possible using the Semaphore class in it. This class is present in java.util.concurrent package. As this class provides all the functionalities by default, you need not crack your heads thinking about what to implement there. Constructors in Java Semaphore class: The Semaphore class in Java contains two contractors: 1. Semaphore(int num)
  • 3. 2. Semaphore(int num, boolean how) The value of num is the number of threads that have permission to access the shared resource. If the boolean value is set to true, the thread receives access to use the resource. Methods present in Java Semaphore class: The Semaphore class contains various built-in methods. The popular methods in this class are: ■ acquire(): Once the permit is available, it returns true; otherwise, it returns false. ■ release(): It releases the permit. ■ available(): This method returns the count of available permits. Types of Java Semaphores: Semaphores are categorized into four types: 1. Counting Semaphores 2. Bounded Semaphores 3. Timed Semaphores 4. Binary Semaphores 1. Counting Semaphores in Java:
  • 4. In situations where we require more than one process to execute simultaneously, the counting semaphores play a major role. It uses the take() method to achieve this. Sample program for Counting Semaphore: public class CoutingSemaphore{ private int signals = 0; public synchronized void take(){ this.signals++; this.notify(); } public synchronized void release() throws InterruptedException{ while(this.signals == 0) wait(); this.signals--; } } 2. Java Bounded semaphores: There is no upper bound value stored in the counting semaphore. To know the count of signals it can store, we use the bounded semaphores to set the upper bound. Sample Program for Bounded Semaphore: public class BoundedSemaphore{ private int signal = 0; private int bound = 0;
  • 5. public BoundedSempahore(int upperBound){ this.bound = upperBound; } public void synchronized take() throws InterruptedException{ while(this.signal == bound) wait(); this.signal++; this.notify++; } public void synchronized release() throws InterruptedException{ while(this.signal == 0) wait(); this.signal--; } } 3. Java Timed Semaphores: While using this semaphore, we can make the thread run for a certain period of time. Once the duration gets over, the times reset, and permits get released. 4. Java Binary Semaphore: The binary semaphores act like the counting semaphores. The only difference is that binary semaphores accept only binary values, either 0 or 1.
  • 6. If the binary semaphore value = 1, the operation succeeds. If the value is 0, the operation does not take place. Semaphores as Locks in Java: In Java, using bounded semaphore as a lock can also take place. To achieve this, we must set the value of the upper bound to 1 and invoke the take() and release() methods. Using these methods, take control of the critical section. Sample snippet to implement using semaphores as locks in java: Ssemaphore sem = new Semaphore(1); sem.take(); try{ //critical section } finally{ sem.release(); } Implementing Semaphore in Java: import java.util.concurrent.*; class FirstCode{ static int count = 0; } class FirstCode2 extends Thread{
  • 7. Semaphore s; String threadName; public FirstCode2(Semaphore s, String threadName){ super(threadName); this.s = s; this.threadName = threadName; }@Override public void run(){ if(this.getName().equals(“Thread1”)){ try{ System.out.println(“Starting ”+ threadName); try{ System.out.println(threadName + “is waiting for permit”); s.acquire(); System.out.println(threadName + “gets a permit”); for(int i = 0; i<5; i++){ SharedResource.count++; System.out.println(threadName+ “:” + SharedResource.count); Thread.sleep(10); } } catch(InterruptedExecution exc){ System.out.println(exc);
  • 8. } System.out.println(threadName + “releases the permit”); s.release(); } else{ System.out.println(“Starting” +threadName); try{ System.out.println(threadName + “gets a permit”); for(int i=0; i<5; i++){ SharedResource.count--; System.out.println(threadName+ “:” + SharedResource.count); Thread.sleep(10); } } catch(InterruptedException exc){ System.out.println(exc); } System.out.println(threadName + “releases the permit”); s.release(); } } } public class SemaphorePgm{
  • 9. public static void main(String args[]) throwsInterruptedException{ Semaphore s = new Semaphore(1); FirstCode2 fc1 = new FirstCode2(s, “Thread1”); FirstCode2 fc2 = new FirstCode2(s, “Thread2”); fc1.start(); fc2.start(); System.out.println(“count: ”+ SharedResource.count); } } Output: Starting Thread1 Starting Thread2 Thread1 is waiting for a permit. Thread2 is waiting for a permit. Thread1 gets a permit. Thread1: 1 Thread1: 2 Thread1: 3 Thread1: 4 Thread1: 5 Thread1 releases the permit. Thread2 gets a permit. Thread2: 4 Thread2: 3 Thread2: 2
  • 10. Thread2: 1 Thread2: 0 Thread2 releases the permit. count: 0 Explanation of the program: This program takes the help of a semaphore to control the access of the count variable, which is a static variable within the Shared class. The Shared.count increments five times by thread fc1 and decreases five times by thread fc2. Now we must prevent these two threads from accessing the Shared.count. to achieve this, access is allowed only after gaining the permit from the controlling semaphore. In this way, only one thread at a time will access the Shared.count as provided in the output. Note that the call to sleep() inside the run() method is within the FirstCode2 class. it proves the access to Shared.count is synchronized by the semaphore. In the run() method, the class to sleep() method enables the thread to pause between each access to Shared.count. Generally, this enables the second thread to run. Yet, due to the presence of semaphore, this second thread must wait until the first thread releases its permit. This takes place only if the first thread completes all its tasks. Therefore, the Shared.count is incremented five times by thread fc1 and decremented fives times by thread fc2. these increments and decrements are not intermixed at the assembly code. In the absence of semaphore, the accesses to Shared.count by both threads would have occurred simultaneously. Also, the increments and decrements would have been intermixed. To validate this, you will notice that the access to Shared.count is no longer synchronized. Therefore, you will not get the count value to be 0 always.
  • 11. Using Semaphores as locks to prevent race conditions: The semaphore helps us in locking access to a resource. Any string that wants to utilize the resource should call the obtain() method first. This should take place before it gets to the asset to gain the bolt. Once the string finishes with the asset, it must call the discharge() method to discharge the bolt. Sample program to implement using semaphores as lock to prevent race condition: class FirstCode{ static int count = 0; } class FirstCode2 extends Thread{ Semaphore s; String threadName; public FirstCode2(Semaphore s, String threadName){ super(threadName); this.s = s; this.threadName = threadName; } @Override public void run(){ if(this.getName().equal(“A”)){ System.out.println(“Starting ”+threadName);
  • 12. try{ System.out.println(threadName + “is waiting for a permit.”); s.acquire(); System.out.println(“threaName ”+ “gets a permit.”); for(int i=0; i<5; i++){ Shared.count++; Sstem.out.println(threadName + “:” + Shared.count); Thread.sleep(10); } }catch(UnterruptedException exc){ System.out.println(exc); } System.out.println(threadName + “releases the permit.”); s.release(); } else{ System.out.println(“Starting” + threadName); try{ System.out.println(threadName + “is waiting for a permit.”); s.acquire(); System.out.println(threadName+ “gets a permit.”); for(int i=0; i<5;i++){ Shared.count--;
  • 13. System.out.println(threadName+ “:” +Shared.count); Thread.sleep(10); } }catch(InterruptedException exc){ System.out.println(exc); } System.out.println(threadName+ “releases the permit.”); s.release(); } } } public class SemaphoreExample{ public static void main(String args[]) throws InterruptedException{ Semaphore s = newSemaphore(1); FirstCode2 fc = new FirstCode2(s, “A”); FirstCode2 fc2 = new FirstCode2(s, “B”); fc.start(); fc2.start(); fc.join(); fc2.join(); System.out.println(“count: ”+Shared.count); } }
  • 14. Output: Starting A Starting B B is waiting for a permit. B gets a permit. A is waiting for a permit. B: -1 B: -2 B: -3 B: -4 B: -5 B releases the permit. A gets a permit. A: -4 A: -3 A: -2 A: -1 A: 0 A releases the permit. count: 0 Summary This is all about semaphore in java. Hope you enjoyed the article.