예제 #1
0
        static void Main(string[] args)
        {
            //print thread starting notification
            Console.WriteLine("Main thread started with initial chicken price $25...\n\n");

            ChickenFarm chicken = new ChickenFarm();
            //new Chicken Farm thread with name farmer
            Thread farmer = new Thread(new ThreadStart(() => chicken.OrderProcessingThread(buffer)))
            {
                Name = "farmer"
            };

            //Start the thread
            farmer.Start();

            Retailer store = new Retailer();

            //Generate the event
            ChickenFarm.priceCut += new priceCutEvent(store.OnSale);
            //Create 5 Retailers
            Thread[] retailers = new Thread[RETAILER_N];
            for (int i = 0; i < RETAILER_N; i++)
            {
                retailers[i]      = new Thread(new ThreadStart(() => store.RetailerThread(buffer)));
                retailers[i].Name = (i + 1).ToString();
                retailers[i].Start();
            }
            farmer.Join();

            //print ending message
            Thread.Sleep(2000);
            Console.WriteLine("Main thread completed");
            Console.ReadKey();
        }
예제 #2
0
        //The function to start Retailer threads
        public void RetailerThread(MultiCellBuffer buffer)
        {
            ChickenFarm chicken = new ChickenFarm();

            //using loop to keep create the orders
            while (chicken.GetPrice() != -1)
            {
                Thread.Sleep(2000);
                //double check the price to avoid something happened during the sleep period
                if (chicken.GetPrice() != -1)
                {
                    //set random order properties
                    long cardNo = rand.Next(5000, 7000);
                    int  amount = rand.Next(1, 5);

                    //add the properties to the object
                    OrderClass newOrder = new OrderClass()
                    {
                        SenderID  = Thread.CurrentThread.Name,
                        CardNo    = cardNo,
                        Amount    = amount,
                        UnitPrice = currPrice
                    };

                    //print out order created message
                    Console.WriteLine("---- New Order Created ----\n" +
                                      "Sender ID: Store {0}\n" +
                                      "Card Number: {1}\n" +
                                      "Amount of Chickens Ordered: {2}\n" +
                                      "Current Unit Price: {3}\n" +
                                      "Order Received Time: {4}\n" +
                                      "=========================================\n",
                                      newOrder.SenderID, newOrder.CardNo, newOrder.Amount, newOrder.UnitPrice, DateTime.Now);
                    //encode the OrderClass object to string
                    EncodeOrder(newOrder);
                }
            }
        }