コード例 #1
0
        public void retailerFunc()
        {
            //for starting thread
            //Check if the priceCutCounter is less than 10
            while (ChickenFarm.priceCutCounter < 10)
            {
                ChickenFarm chicken = new ChickenFarm();

                Thread.Sleep(500);
                Int32 p = chicken.getPrice();
                //Console.WriteLine("Store {0} has everyday low price: ${1} each", Thread.CurrentThread.Name, p);
                //Check if there is a price cut
                if (ChickenFarm.priceCutBool)
                {
                    //Create a new order and set the properties
                    Order order = new Order();
                    order.Amount       = rng.Next(0, 5);
                    order.SenderId     = Thread.CurrentThread.Name;
                    order.CardNo       = rng.Next(5000, 7000);
                    order.ChickenPrice = p;
                    order.TimeStamp    = GetTimestamp(DateTime.Now);
                    //Encode the order into String
                    String encodedOrder = encode(order);
                    //Send the order buffer cell
                    submitOrder(encodedOrder);
                    //put the thread to sleep for 2 seconds
                    Thread.Sleep(2000);
                }
            }
        }
コード例 #2
0
        public void farmerFunc()
        {
            while (priceCutCounter < 10)
            {
                Thread.Sleep(500);
                // Take the order from the queue of the orders;
                // Decide the price based on the orders
                Int32 p = ChickenFarm.pricingModel();
                // Console.WriteLine("New Price is {0}", p);
                ChickenFarm.changePrice(p);

                Console.WriteLine("New Price {0}", p);

                //Read the cell if the multi cell buffer is not empty
                if (MultiCellBuffer.counter != 0)
                {
                    //Initialise a new order process object
                    OrderProcessing orderProcess = new OrderProcessing();
                    //Read the cell
                    String orderStr = MultiCellBuffer.getCell();
                    //Decode the order string into order object
                    Order order = ChickenFarm.decode(orderStr);
                    //Start a new orderprocess thread
                    Thread thread = new Thread(() => orderProcess.processOrder(order));
                    thread.Name = "Order " + ChickenFarm.orderNum;
                    thread.Start();
                    //Increment the order number
                    ChickenFarm.orderNum = ChickenFarm.orderNum + 1;
                }
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            //Initialise the multi cell buffer
            for (int i = 0; i < 2; i++)
            {
                MultiCellBuffer.buffer[i] = "";
            }

            //Initialise the chicken farm object
            ChickenFarm chicken = new ChickenFarm();
            //Create a thread for chicken farm
            Thread farmer = new Thread(new ThreadStart(chicken.farmerFunc));

            farmer.Start();         // Start one farmer thread

            //Initialise the relailer object
            Retailer chickenstore = new Retailer();

            //Event handler when there is a price cut
            ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale);
            //Event handler when a order is completed
            OrderProcessing.completeOrder += new completeOrderEvent(chickenstore.completeOrder);
            //Create 5 threads for 5 retailers
            Thread[] retailers = new Thread[5];
            for (int i = 0; i < 5; i++)
            {   // Start N retailer threads
                retailers[i]      = new Thread(new ThreadStart(chickenstore.retailerFunc));
                retailers[i].Name = "Retailer" + (i + 1).ToString();
                retailers[i].Start();
            }

            //Check if 10 price cuts have occured
            while (true)
            {
                if (ChickenFarm.priceCutCounter >= 10)
                {
                    //Terminate the chicken farm thread
                    farmer.Abort();
                    Console.WriteLine("Chicken farm thread terminated after 10 price cuts");
                    for (int i = 0; i < 5; i++)
                    {
                        // Terminate N retailer threads
                        retailers[i].Abort();
                        Console.WriteLine("Retailer{0} farm thread terminated after 10 price cuts", i);
                    }
                    break;
                }
            }
        }