static void Main(string[] args) { ChickenFarm chicken = new ChickenFarm(); //caller is in the chickenFarmer, but function resides in the Retailer Thread farmer = new Thread(new ThreadStart(chicken.farmerFunc)); farmer.Start(); // Start one farmer thread Retailer chickenstore = new Retailer(); ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale); //registers the chickOnSale() method (event handler in all the Retailers // to the priceCut event in the ChickenFarm thread //Since priceCut event is defined as a delegate, that can call all the // subscribed event handlers when the event occurs //The changePrice() method searches if there is a subscriber by priceCut!=NULL //eventhandler is only used when an event happens (or there is a price change) Thread[] retailers = new Thread[3]; for (int i = 0; i < 3; i++) //N= 3here //creates stores 1,2,3 { // Start N retailer threads retailers[i] = new Thread(new ThreadStart(chickenstore.retailerFunc)); retailers[i].Name = (i + 1).ToString(); //makes names for retailers(Threads) retailers[i].Start(); //start thread } }
public void farmerFunc() { for (Int32 i = 0; i < 50; i++) { Thread.Sleep(500); Int32 p = rng.Next(5, 10); ChickenFarm.changePrice(p); } }
public void retailerFunc() { ChickenFarm chicken = new ChickenFarm(); for (Int32 i = 0; i < 10; i++) { Thread.Sleep(1000); Int32 p = chicken.getPrice(); Console.WriteLine("Store{0} has everyday low price: ${1} each", Thread.CurrentThread.Name, p); } }
public void retailerFunc() //for starting thread { ChickenFarm chicken = new ChickenFarm(); for (Int32 i = 0; i < 10; i++) { Thread.Sleep(1000); //checks the price every 1000 milliseconds Int32 p = chicken.getPrice(); Console.WriteLine("Store{0} has everyday low price: ${1} each", Thread.CurrentThread.Name, p); // Thread.CurrentThread.Name prints thread name } }
public void farmerFunc() { for (Int32 i = 0; i < 50; i++) { Thread.Sleep(500); // Take the order from the queue of the orders; // Decide the price based on the orders Int32 p = rng.Next(5, 10); //generates price from 5, 10 every 500milliseconds // Console.WriteLine("New Price is {0}", p); ChickenFarm.changePrice(p); } }
static void Main(String[] args) { ChickenFarm chicken = new ChickenFarm(); Thread farmer = new Thread(new ThreadStart(chicken.farmerFunc)); farmer.Start(); Retailer chickenstore = new Retailer(); ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale); Thread[] retailers = new Thread[5]; for (int i = 0; i < 3; i++) { retailers[i] = new Thread(new ThreadStart(chickenstore.retailerFunc)); retailers[i].Name = (i + 1).ToString(); retailers[i].Start(); } }