static void Main(string[] args) { // Create bottle buffers BottleBuffer producerBuffer = new BottleBuffer(30); BottleBuffer faxeBuffer = new BottleBuffer(20); BottleBuffer beerBuffer = new BottleBuffer(20); // Create the producer and the consumer splitter Producer producer = new Producer(producerBuffer); ConsumerSplitter conSplit = new ConsumerSplitter(producerBuffer, beerBuffer, faxeBuffer); // Creates the consumers Consumer faxeConsumer = new Consumer(faxeBuffer, "Faxe Consumer"); Consumer beerConsumer = new Consumer(beerBuffer, "Beer Consumer"); // Start all the threads producer.Start(); conSplit.Start(); faxeConsumer.Start(); beerConsumer.Start(); // Wait for all threads to be done producer.Join(); conSplit.Join(); faxeConsumer.Join(); beerConsumer.Join(); Console.ReadKey(); }
/// <summary> /// Adds a bottle to a buffer /// </summary> /// <param name="buffer"></param> /// <param name="bottle"></param> private void AddBottleToBuffer(BottleBuffer buffer, Bottle bottle) { Monitor.Enter(buffer.Lock); try { buffer.AddBottleInFront(bottle); if (buffer.GetItemsInBuffer() == 1) { Monitor.PulseAll(buffer.Lock); } } finally { Monitor.Exit(buffer.Lock); } }
public Producer(BottleBuffer buffer) { BottleBuffer = buffer; }
public ConsumerSplitter(BottleBuffer ingoingBuffer, BottleBuffer beerBuffer, BottleBuffer faxeBuffer) { InBuffer = ingoingBuffer; BeerBuffer = beerBuffer; FaxeBuffer = faxeBuffer; }
public Consumer(BottleBuffer ingoingBuffer, string name) { InBuffer = ingoingBuffer; Name = name; }