private Encoder orderEncoder = new Encoder(); //init new encoder public void ChickenRetailer() //thread continues until ChickenFarm thread terminates { ChickenFarm chicken = new ChickenFarm(); while (farmIsProducing) { if (priceCut) //if there has been a price cut, have thread create order, then wait { createOrder(Thread.CurrentThread.Name, newPrice); Thread.Sleep(1000); //so retailers can't continuously place orders during a price cut } //else wait for price cut } //terminate thread Console.WriteLine("Store {0} has closed for the day.", Thread.CurrentThread.Name); }
static void Main(string[] args) { ChickenFarm chickenFarm = new ChickenFarm(); //init chicken farm //Initialize Farmer (Producer) thread Thread farmer = new Thread(new ThreadStart(chickenFarm.ChickenFarmer)); farmer.Name = "Farmer"; farmer.Start(); //Start farmer thread //Initialize Retailer (Consumer) threads Retailer chickenStore = new Retailer(); //init ChickenStore ChickenFarm.priceCut += new priceCutEvent(chickenStore.chickenPriceCut); //Subscribe chickenStore to Price Cut event ChickenFarm.priceRaise += new priceRaiseEvent(chickenStore.chickenPriceRaise); //Subscribe to Price Raise event OrderProcessing.orderConfirmation += new orderConfirmationEvent(chickenStore.orderConfirmed); Thread[] retailerThreads = new Thread[MAX_NUM_RETAILERS]; //init retailerThreads array for (int i = 0; i < MAX_NUM_RETAILERS; i++) { retailerThreads[i] = new Thread(new ThreadStart(chickenStore.ChickenRetailer)); retailerThreads[i].Name = (i + 1).ToString(); retailerThreads[i].Start(); } //Wait for 10 price cuts while (farmer.IsAlive) { ; } //Tell retailers that farm is not producing anymore chickenStore.setFarmIsProducing(false); //wait for retailers to close for (int i = 0; i < MAX_NUM_RETAILERS; i++) { while (retailerThreads[i].IsAlive) { mb.getOneCell(); //empty the buffer } } //Wait for user input to end program Console.WriteLine("Enter any key to end the program..."); Console.ReadLine(); }