private static (Thread[], Thread[]) StartParty(Knight[] knights, DrinkingBout drinkingBout)
        {
            var kThreads = StartKnightsTittleTattle(knights);
            var wThreads = ServeCucumbersAndNonAlcoholicWine(drinkingBout);

            return(kThreads, wThreads);
        }
Beispiel #2
0
        public Knight(int idx, int r_idx, int l_idx,
                      Rostrum rostrum, DrinkingBout drinkingBout)
        {
            Idx   = idx;
            R_Idx = r_idx;
            L_Idx = l_idx;

            this.rostrum      = rostrum;
            this.drinkingBout = drinkingBout;
        }
        static void Main(string[] args)
        {
            Rostrum      rostrum      = new Rostrum();
            DrinkingBout drinkingBout = new DrinkingBout();

            Knight[] knights = InitializeKnights(rostrum, drinkingBout);
            rostrum.RegisterKnights(knights);


            var threads = StartParty(knights, drinkingBout);

            StopParty(threads.Item1, threads.Item2);
        }
        private static Knight[] InitializeKnights(Rostrum rostrum, DrinkingBout drinkingBout)
        {
            var knights = new Knight[Config.NumberOfKnights];

            for (int i = 1; i < Config.NumberOfKnights - 1; i++)
            {
                knights[i] = new Knight(i, i + 1, i - 1, rostrum, drinkingBout);
            }
            knights[0] = new Knight(0, 1, Config.NumberOfKnights - 1, rostrum, drinkingBout);
            knights[Config.NumberOfKnights - 1] =
                new Knight(Config.NumberOfKnights - 1, 0, Config.NumberOfKnights - 2, rostrum, drinkingBout);

            return(knights);
        }
        private static Thread[] ServeCucumbersAndNonAlcoholicWine(DrinkingBout drinkingBout)
        {
            var waiterOne = new Waiter(drinkingBout, WaiterType.NonAlcoholicWineWaiter);
            var waiterTwo = new Waiter(drinkingBout, WaiterType.CucumberWaiter);

            var waiterOneThread = new Thread(() =>
            {
                waiterOne.Serve();
            });

            var waiterTwoThread = new Thread(() =>
            {
                waiterTwo.Serve();
            });

            waiterOneThread.Start();
            waiterTwoThread.Start();

            return(new Thread[]
            {
                waiterOneThread,
                waiterTwoThread
            });
        }
 public Waiter(DrinkingBout drinkingBout, WaiterType type)
 {
     this.drinkingBout = drinkingBout;
     this.type         = type;
 }