class Buffer {
const int size = 4;
int n = 0;
public void Put(char ch)
{
lock(this)
{
while (n == size)
Monitor.Wait(this);
n++;
Monitor.Pulse(this);
}
}
public void Get(char ch)
{
lock(this)
{
while (n == 0)
Monitor.Wait(this);
n--;
Monitor.Pulse(this);
}
}
}
I found the above program on the web. It shows how a producer/consumer works
with the Monitor class. It says if the producer is faster than the consumer
then I can expect the following result:
Put
Put
Put
Put
Get
Put
Get
....
and if the consumer is faster:
Put
Get
Put
Get
....
My question is this: I don't understand the result when the producer is
faster. How can you have too many Puts after each other?