In concurrent programming, a semaphore is a synchronization aid that manages access to a shared resource by multiple threads. It maintains a set of permits that threads must acquire before accessing the resources like databases, file systems, or network connections.
In the previous tutorial, we learned about binary semaphores, which can be used to control access to a single copy of a resource using the counter value either 0 or 1. However, semaphores can also be used when you need to protect various copies of a resource that can be executed by more than one thread simultaneously. In this example, we will learn how to use a semaphore to protect more than one resource copy.
Let’s revisit the semaphore concept before moving ahead.
1. How does a Semaphore Work?
A semaphore can be visualized as a counter that can be incremented or decremented. You initialize the semaphore with a number, such as 5.
- This semaphore can be decremented a maximum of five times in a row until the counter reaches 0.
- Once the counter is zero, you can increment it to a maximum of five times to make it 5.
- The counter value of semaphore MUST always be inside limit 0 <= n >= 5 (in our case).
Obviously, semaphores are more than just counters. They can make threads wait when the counter value is zero, i.e., they act as Locks with counter functionality.
Regarding multi-threading, when a thread wants to access one of the shared resources (guarded by a semaphore), it must first acquire the semaphore.
If the internal counter of the semaphore is greater than 0, the semaphore decrements the counter and allows access to the shared resource. Otherwise, if the counter of the semaphore is 0, the semaphore puts the thread to sleep until the counter is greater than 0. A value of 0 in the counter means other threads use all the shared resources, so the thread that wants to use one of them must wait until one is free.
A thread can proceed only if it successfully acquires a permit. When a thread finishes, it must release the permit.
Read More: How to Use Locks in Java
2. How to Use a Semaphore?
To demonstrate the concept, we will use a semaphore to control 3 printers that can print multiple documents simultaneously.
2.1. PrintingJob
This class represents an independent printing job that could be submitted to the printer queue. And from the queue, it can be picked up by any printer and performed printing job. This class implements Runnable
interface, so that printer can execute it when it’s turn come.
2.2. PrinterQueue
The PrinterQueue class represents the printer queue/ printer. This class has 3 main attributes which control the logic of selecting a free printer out of 3 printers and lock it for printing a job. After printing the document, printer is released so that it is again free and available for printing a new job from print queue.
This class has two methods getPrinter()
and releasePrinter()
which are responsible for acquiring a free printer and putting it back in the free printer pool.
Another method printJob()
actually does the core job, i.e., acquiring a printer, executing the print job, and then releasing the printer.
It uses below two variables for doing the job:
- semaphore: This variable keeps track of no. of printers used at any point in time.
- printerLock: Used for locking the printer pool before checking/acquiring a free printer out of three available printers.
Read More : How to use binary semaphore?
2.3. Demo
Let’s test our printer program:
In the above example, the object is created using 3 as the constructor’s parameter. The first three threads that call the acquire()
method will get access to printers while the rest will be blocked. When a thread finishes the critical section and releases the semaphore, another thread will acquire it.
In the printJob()
method, the thread gets the index of the printer assigned to print this job.
The program output:
That’s all for this simple yet important concept. Drop me your questions and comments, if any.
Happy Learning !!
Comments