Exemple #1
0
        public static void Consumidor(Almacen almacen)
        {
            Random random = new Random();

            while (true)
            {
                int elemento = almacen.Consumir();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"Se consume el numero {elemento}.");
                Console.ResetColor();
                Thread.Sleep(TimeSpan.FromSeconds(random.Next(8, 10)));
            }
        }
Exemple #2
0
        public static void Productor(Almacen almacen)
        {
            Random random = new Random();

            while (true)
            {
                int elemento = random.Next(0, 100);
                almacen.Producir(elemento);
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine($"Se produce el numero {elemento}.");
                Console.ResetColor();
                Thread.Sleep(TimeSpan.FromSeconds(random.Next(0, 2)));
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            Almacen almacen = new Almacen(10);

            Thread hiloProductor   = new Thread(() => Productor(almacen));
            Thread hiloConsumidor1 = new Thread(() => Consumidor(almacen));
            Thread hiloConsumidor2 = new Thread(() => Consumidor(almacen));

            hiloProductor.Start();
            hiloConsumidor1.Start();
            hiloConsumidor2.Start();
            hiloProductor.Join();
            hiloConsumidor1.Join();
            hiloConsumidor2.Join();
        }