The key to this example is the printJob() method of the PrintQueue class. This method shows three steps you must follow when you use a semaphore to implement a critical section and protect access to a shared resource:
- First, you acquire the semaphore with the acquire() method.
- Then, you do the necessary operations with the shared resource.
- Finally, release the semaphore with the release() method.
Another important point in this example is the constructor of the PrintQueue class and the initialization of the Semaphore object. You pass the value 3 as the parameter of this constructor, so you are creating a semaphore that will protect three resources. The first three threads that call the acquire() method will get access to ...