Ejemplo n.º 1
0
        public void Start()
        {
            Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    Bottle b = new Bottle();
                    _sem.WaitOne();     // wait for ready
                    Console.WriteLine(_buf.Peek());
                    _buf.Insert(b);
                    Console.WriteLine("prod buf:" + _buf.Peek());
                    Thread.Sleep(25);
                }
            }
                     );

            Task.Run(() =>
            {
                for (int i = 0; i < 12; i++)
                {
                    Console.WriteLine(_buf.Peek());
                    Bottle b = _buf.Take();
                    Console.WriteLine("Cons: " + b + "  peek " + _buf.Peek());
                    _sem.Release();
                }
            }
                     );
        }
Ejemplo n.º 2
0
 private static void Reader(BoundedBuffer buffer)
 {
     for (int i = 0; i < 10; i++)
     {
         buffer.Take();
     }
 }
Ejemplo n.º 3
0
        public void TryPut_WithEmptyBuffer()
        {
            var success = boundedBuffer.TryPut(42, timeout);

            Assert.IsTrue(success);
            Assert.AreEqual(42, boundedBuffer.Take());
        }
Ejemplo n.º 4
0
        public void Start()
        {
            Trace.TraceInformation("Simulation Started");

            // Start generate bottles
            Thread tg = new Thread(() => GenerateBottles(NUMBER_OF_BOTTLES));

            tg.Start();

            // Start washing bottles
            Task.Run(() =>
            {
                Trace.TraceInformation($"{NUMBER_OF_WASHING_THREADS} washing threads started");
                Parallel.For(0, NUMBER_OF_WASHING_THREADS, (i) => WashBottle(i));
            });

            // Start Filling bottles
            Task.Run(() =>
            {
                Trace.TraceInformation($"{NUMBER_OF_FILLING_TOPPING_THREADS} filling threads started");
                Parallel.For(0, NUMBER_OF_FILLING_TOPPING_THREADS, (i) => FillBottle(i));
            });

            // Start Topping bottles
            Task.Run(() =>
            {
                Trace.TraceInformation($"{NUMBER_OF_FILLING_TOPPING_THREADS} topping threads started");
                Parallel.For(0, NUMBER_OF_FILLING_TOPPING_THREADS, (i) => TopBottle(i));
            });

            // Start Packing bottles
            Task.Run(() =>
            {
                Trace.TraceInformation($"{NUMBER_OF_PACKING_THREADS} packing threads started");
                Parallel.For(0, NUMBER_OF_PACKING_THREADS, (i) => BoxingBottle(i));
            });

            /*
             * Get Boxes
             */
            int expectedNoOfBoxes = NUMBER_OF_BOTTLES / 24 - NUMBER_OF_PACKING_THREADS;
            int countedBoxes      = 0;

            while (countedBoxes <= expectedNoOfBoxes)
            {
                List <Bottle> box = boxOfBottles.Take();
                Trace.TraceInformation($"Box no {++countedBoxes} received with {box.Count} bottles");
            }
            Trace.TraceInformation("Simulation ended");

            // nice close of all tracing
            foreach (TraceListener tl in Trace.Listeners)
            {
                tl?.Close();
            }

            Thread.Sleep(10000);
            System.Environment.Exit(0);
        }
Ejemplo n.º 5
0
        /*
         * Washing Bottles
         */
        private void WashBottle(int nr)
        {
            String myName = "Wash" + nr;

            Trace.TraceInformation($"{myName} is started");

            while (true)
            {
                Bottle b = unwashedBottles.Take();
                Thread.Sleep(5 + rnd.Next(15));
                b.State = "washed";
                TraceBottle(b);
                washedBottles.Insert(b);
            }
        }
Ejemplo n.º 6
0
        /*
         * Filling Bottles
         */
        private void FillBottle(int nr)
        {
            String myName = "Fill" + nr;

            Trace.TraceInformation($"{myName} is started");

            while (true)
            {
                toppedIsReady[nr].WaitOne();
                Bottle b = washedBottles.Take();
                Thread.Sleep(15 + rnd.Next(15));
                b.State = "Filled";
                TraceBottle(b);
                filledBottles[nr].Insert(b);
            }
        }
Ejemplo n.º 7
0
        /*
         * Packing Bottles
         */
        private void BoxingBottle(int nr)
        {
            String myName = "Counting" + nr;

            Trace.TraceInformation($"{myName} is started");

            List <Bottle> bottleBox;

            while (true)
            {
                bottleBox = new List <Bottle>();
                for (int i = 0; i < 24; i++)
                {
                    Bottle b = toppedBottles.Take();
                    Thread.Sleep(5 + rnd.Next(10));
                    b.State = "Boxing";
                    bottleBox.Add(b);
                    TraceBottle(b);
                }
                boxOfBottles.Insert(bottleBox);
            }
        }