... continued
This lesson explains how to solve the producer-consumer problem using a mutex.
We'll cover the following...
Busy wait solution using Lock
In the previous lesson, we solved the consumer producer problem using the synchronized
keyword, which is equivalent of a monitor in Java. Let's see how the implementation would look like, if we were restricted to using a mutex. There's no direct equivalent of a theoretical mutex in Java as each object has an implicit monitor associated with it. For this question, we'll use an object of the Lock
class and pretend it doesn't expose the wait()
and notify()
methods and only provides mutual exclusion similar to a theoretical mutex. Without the ability to wait or signal the implication is, a blocked thread will constantly poll in a loop for a predicate/condition to become true before making progress. This is an example of a busy-wait solution.
Let's start with the ...