... continued

This lesson explains how to solve the producer-consumer problem using semaphores.

We'll cover the following...

Solution using semaphores

We can also implement the bounded buffer problem using a semaphore. For this problem, we'll use an instance of the CountingSemaphore that we implement in one of the later problems. A CountingSemaphore is initialized with a maximum number of permits to give out. A thread is blocked when it attempts to release the semaphore when none of the permits have been given out. Similarly, a thread blocks when attempting to acquire a semaphore that has all the permits given out. In contrast, Java's implementation of Semaphore can be signaled (released) even if none of the permits, the Java semaphore was initialized with, have been used. Java's semaphore has no upper bound and can be released as many times as desired to increase the number of permits. Before proceeding forward, it is suggested to complete the CountingSemaphore lesson. ...