Exemple #1
0
        static void Main(string[] args)
        {
            SynchronizedRandomGenerator numGen = new SynchronizedRandomGenerator(0, 100);
            int numberOfCars = 0;

            do
            {
                Console.WriteLine($"Hello. Please write a number of cars to participate in the race: ");
            }while (!int.TryParse(Console.ReadLine(), out numberOfCars));

            List <Car> cars = new List <Car>();

            for (int i = 0; i < numberOfCars; i++)
            {
                cars.Add(new Car(i + 1, numGen, 1000));
            }
            foreach (Car car in cars)
            {
                Thread thread = new Thread(car.Race);
                _allThreads.Add(thread);
                thread.Start();
            }
            WaitUntilAllThreadsComplete();
            Console.WriteLine("\n************ All cars have finished! ************\n" +
                              "Exiting in 5 seconds...");
            Thread.Sleep(5000);
            Console.ReadLine();
        }
Exemple #2
0
 public Car(int carId, SynchronizedRandomGenerator randomGenerator, int destKm)
 {
     _carId   = carId;
     _numGen  = randomGenerator;
     _destKm  = destKm;
     _totalKm = 0;
 }