예제 #1
0
        static void Main(string[] args)
        {
            ChickenFarm     chicken   = new ChickenFarm();
            MultiCellBuffer buffer    = new MultiCellBuffer();
            OrderProcessing processor = new OrderProcessing();

            //Names of threads that represent retailers
            string[] retailerNames = { "Bob's Chickens", "Chickens R Us", "Turkey Jerky",
                                       "JamJam inc",     "Enterprise Chickens" };
            Thread   farmer = new Thread(new ThreadStart(chicken.runFarmer));

            farmer.Start();
            Retailer chickenstore = new Retailer();

            //retailers subscribe to price cut event
            ChickenFarm.priceCut += new priceCutEvent(chickenstore.chickenOnSale);
            //retailer threads subscribe to farmer thread termination event to stop retailer threads
            ChickenFarm.destroyThreads += new destroyThreadsEvent(chickenstore.destroyThreads);
            //retailer subscribes to order confirmation event
            OrderProcessing.OrderConfirmation += new OrderConfirmationEvent(chickenstore.confirmation);
            //start retailer threads and assign names
            Thread[] retailers = new Thread[5];
            for (int i = 0; i < 5; i++)
            {
                retailers[i]      = new Thread(new ThreadStart(chickenstore.runRetailer));
                retailers[i].Name = retailerNames[i];
                retailers[i].Start();
            }
            //release semaphore to allow threads access to the buffer
            buffer.releaseSemaphore();
            //hold console open after completion
            Console.Read();
        }
예제 #2
0
        public void AirlineFunc()
        {
            while (priceCutCounter <= 20)
            {
                Thread.Sleep(100);
                GenerateTicketPrice();

                // Read an order from the buffer
                String orderStr = MainProgram.orderBuffer.GetOneCell(Thread.CurrentThread.Name);

                // If an order for the airline was received, process order
                if (orderStr != null)
                {
                    OrderClass order = Decoder.DecodeOrder(orderStr);
                    MainProgram.orderBuffer.sem.Release(1);
                    orderCounter++;
                    ticketsAvailable -= order.GetAmount();
                    OrderProcessing orderProc = new OrderProcessing(order);
                    Thread orderProcThread = new Thread(new ThreadStart(orderProc.ProcessNewOrder));
                    orderProcThread.Start();
                }
            }
            MainProgram.airlineThreadCount--;
        }
예제 #3
0
        public void AirlineFunc()
        {
            while (priceCutCounter <= 20)
            {
                Thread.Sleep(100);
                GenerateTicketPrice();

                // Read an order from the buffer
                String orderStr = MainProgram.orderBuffer.GetOneCell(Thread.CurrentThread.Name);

                // If an order for the airline was received, process order
                if (orderStr != null)
                {
                    OrderClass order = Decoder.DecodeOrder(orderStr);
                    MainProgram.orderBuffer.sem.Release(1);
                    orderCounter++;
                    ticketsAvailable -= order.GetAmount();
                    OrderProcessing orderProc       = new OrderProcessing(order);
                    Thread          orderProcThread = new Thread(new ThreadStart(orderProc.ProcessNewOrder));
                    orderProcThread.Start();
                }
            }
            MainProgram.airlineThreadCount--;
        }
        static void Main(string[] args)
        {
            int noOfBufferCells = 3; // Buffer Capacity- Number of cells in Multicellbuffer
            int N = 5; //Number of Reatilers

            ChickenFarm chickenFarm = new ChickenFarm();
            Thread chickenFarm_thread = new Thread(new ThreadStart(chickenFarm.farmerFunc));

            CreditCardHelper creditCardHelper = new CreditCardHelper(N);
            MultiCellBuffer buffer = new MultiCellBuffer(noOfBufferCells);

            buffer.bufferSemaphore.Release(noOfBufferCells);
            OrderProcessing orderProcessing = new OrderProcessing(buffer, creditCardHelper);

            chickenFarm_thread.Name = "ChickenFarm";
            chickenFarm_thread.Start();
            Retailer retailer = null;
            for (int i = 0; i < N; i++)
            {
                retailer = new Retailer(i, buffer, creditCardHelper);
                chickenFarm.priceCut += new priceCutEvent(retailer.chickensOnSale);
            }
            buffer.newOrder += new newOrderEvent(orderProcessing.orderProcessing);
            orderProcessing.orderCompleted += new orderCompletedEvent(retailer.confirmationFunc);
            Console.ReadKey();
        }