コード例 #1
0
        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();
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
 public Producer(BottleBuffer buffer)
 {
     BottleBuffer = buffer;
 }
コード例 #4
0
 public ConsumerSplitter(BottleBuffer ingoingBuffer, BottleBuffer beerBuffer, BottleBuffer faxeBuffer)
 {
     InBuffer   = ingoingBuffer;
     BeerBuffer = beerBuffer;
     FaxeBuffer = faxeBuffer;
 }
コード例 #5
0
ファイル: Consumer.cs プロジェクト: blackout1471/H2
 public Consumer(BottleBuffer ingoingBuffer, string name)
 {
     InBuffer = ingoingBuffer;
     Name     = name;
 }