// for starting thread public void travelAgencyFunc() { Airline air = new Airline(); // creates an airline object for checking the prices int count = 0; do// loops 10 times to periodically check if there is a price cut { Console.WriteLine("COUNT = " + count); Thread.Sleep(1000); // wait for the confirm buffer to be released int month = DateTime.Now.Month; int day = DateTime.Now.Day; int year = DateTime.Now.Year; int hour = DateTime.Now.Hour; int min = DateTime.Now.Minute; int sec = DateTime.Now.Second; double orderTotal = 0; // initializes it to zero in order to go through a loop and keep checking the confirmation buffer // Monitor.Wait(Program.confirm); // wait for the confirm buffer to be released while (orderTotal == 0) { Thread.Sleep(1000); // Program.rwlock.AcquireReaderLock(300); // try{ Console.WriteLine("CHECK NAME: " + Thread.CurrentThread.Name); orderTotal = Program.confirm.getCell(Thread.CurrentThread.Name); // } // finally{ // Program.rwlock.ReleaseReaderLock(); // } } count++; Console.WriteLine("Order processed for $" + orderTotal + " from " + Thread.CurrentThread.Name + ". Order finished processing at: " + month + "/" + day + "/" + year + " @ " + hour + ":" + min + ":" + sec + " and took " + (DateTime.Now.Second - sec) + " seconds"); // print out the timestamp the order was finished processing } while (count < 20); }
static void Main(string[] args) { // change Airline air = new Airline(); // create an airline to pass the airline function for threading // change // Instantiate 3 airline threads and then start them Thread airline1 = new Thread(new ThreadStart(() => air.priceModel("airline1"))); // start the thread with the passed-in parameter airline1 string Thread airline2 = new Thread(new ThreadStart(() => air.priceModel("airline2"))); // start the thread with the passed-in parameter airline2 string Thread airline3 = new Thread(new ThreadStart(() => air.priceModel("airline3"))); // start the thread with the passed-in parameter airline3 string airline1.Name = "airline 1"; airline2.Name = "airline 2"; airline3.Name = "airline 3"; airline1.Start(); airline2.Start(); airline3.Start(); // Instantiate a travel agency object in order to use its methods TravelAgency travel = new TravelAgency(); Airline.priceCut += new priceCutEvent(travel.ticketsOnSale); // have the travel agency method subscribe to airline event Thread[] travelAgencies = new Thread[5]; // create 5 travel agency threads for (int i = 0; i <= 4; i++) // N = 5 (for five travel agency threads) { // Start 5 travel agency threads travelAgencies[i] = new Thread(new ThreadStart(travel.travelAgencyFunc)); travelAgencies[i].Name = "travel agency " + (i+1).ToString(); travelAgencies[i].Start(); } // create buffer classes, etc. }
public static void Main() { // Initialize Airline and TravelAgency objects Airline airline0 = new Airline(); Airline airline1 = new Airline(); Airline airline2 = new Airline(); TravelAgency travelAgency = new TravelAgency(); // Initialize and start Threads Thread[] airlines = new Thread[3]; for (int i = 0; i < 3; i++) { airlines[i] = new Thread(new ThreadStart(airline0.AirlineFunc)); airlines[i].Name = "AIRLINE " + i; airlines[i].Start(); airlineThreadCount++; } Thread[] travelAgencies = new Thread[5]; for (int i = 100; i < 105; i++) { travelAgencies[i - 100] = new Thread(new ThreadStart(travelAgency.TravelAgencyFunc)); travelAgencies[i - 100].Name = "TRAVEL AGENCY " + i; travelAgencies[i - 100].Start(); } // Bind TravelAgency event handler to Airline event Airline.PriceCutEvent += new PriceCutDelegate(travelAgency.TicketPriceCut); // Wait for all threads to start and block on the semaphore Thread.Sleep(500); // Release 3 slots to start program orderBuffer.sem.Release(3); confirmationBuffer.sem.Release(3); Console.ReadLine(); }