Esempio n. 1
0
        public void farmerFunc()
        {
            // Timestamp used to mark when the thread begins.
            DateTime startTime = new DateTime();

            startTime = DateTime.Now;

            System.Timers.Timer eventTimer = new System.Timers.Timer();
            eventTimer.Elapsed  += new ElapsedEventHandler(recountChickens);
            eventTimer.Interval  = 50;
            eventTimer.AutoReset = true;
            eventTimer.Start();

            while (priceCutCounter < 10)
            {
                Thread.Sleep(100);

                Console.Out.WriteLine("ChickenFarm has {0} chickens left", numChickens);
                newPrice = PricingModel.reevaluatePrice();
                ChickenFarm.changePrice(newPrice);

                lock (Program.orderBuffer)
                {
                    if (Program.orderBuffer.IsFull())
                    {
                        orderProcessingObject = new OrderProcessing();
                        for (int i = 0; i < Program.NUM_RETAILERS - 1; i++)
                        {
                            String s = Program.orderBuffer.Consume(i);
                            if (s != null)
                            {
                                Order cfOrder = Decoder.decode(s);
                                orderProcessingObject.setOrder(cfOrder);
                                Thread orderProcessingThread = new Thread(new ThreadStart(orderProcessingObject.orderProcessing));
                                orderProcessingThread.Start();
                            }
                        }
                    }
                }
            }

            // Timestamp used to mark when the thread ends.
            DateTime endTime = new DateTime();

            endTime = DateTime.Now;
            // Time span between start time and end time (thread duration)
            TimeSpan span = endTime.Subtract(startTime);

            System.Console.WriteLine("Total Elapsed Time for ChickenFarm Thread: {0} ms", span.TotalMilliseconds);
            System.Console.WriteLine("ChickenFarm DIES! :(");

            // do not wait for OrderProcessing threads to terminate! This is a poor design
            // choice if this software was implemented in real life, but for the sake of
            // finishing the assignment on time, we will not deal with it.
        }
Esempio n. 2
0
        public void farmerFunc()
        {
            // Timestamp used to mark when the thread begins.
            DateTime startTime = new DateTime();
            startTime = DateTime.Now;

            System.Timers.Timer eventTimer = new System.Timers.Timer();
            eventTimer.Elapsed += new ElapsedEventHandler(recountChickens);
            eventTimer.Interval = 50;
            eventTimer.AutoReset = true;
            eventTimer.Start();

            while (priceCutCounter < 10)
            {
                Thread.Sleep(100);

                Console.Out.WriteLine("ChickenFarm has {0} chickens left", numChickens);
                newPrice = PricingModel.reevaluatePrice();
                ChickenFarm.changePrice(newPrice);

                lock (Program.orderBuffer)
                {
                    if (Program.orderBuffer.IsFull())
                    {
                        orderProcessingObject = new OrderProcessing();
                        for (int i = 0; i < Program.NUM_RETAILERS - 1; i++)
                        {
                            String s = Program.orderBuffer.Consume(i);
                            if (s != null)
                            {
                                Order cfOrder = Decoder.decode(s);
                                orderProcessingObject.setOrder(cfOrder);
                                Thread orderProcessingThread = new Thread(new ThreadStart(orderProcessingObject.orderProcessing));
                                orderProcessingThread.Start();
                            }
                        }
                    }
                }
            }

            // Timestamp used to mark when the thread ends.
            DateTime endTime = new DateTime();
            endTime = DateTime.Now;
            // Time span between start time and end time (thread duration)
            TimeSpan span = endTime.Subtract(startTime);
            System.Console.WriteLine("Total Elapsed Time for ChickenFarm Thread: {0} ms", span.TotalMilliseconds);
            System.Console.WriteLine("ChickenFarm DIES! :(");

            // do not wait for OrderProcessing threads to terminate! This is a poor design
            // choice if this software was implemented in real life, but for the sake of
            // finishing the assignment on time, we will not deal with it.
        }