jueves, 29 de marzo de 2012

Práctica de Hilos.

Instrucciones:


  • Mejore el siguiente código.
  • Haga pruebas con él.
  • Saque conclusiones del uso de hilos.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace simulacionCaja
{
    class caja2
    {
        Queue<int> clientes;// = new Queue<int>();
        int op = 0; //número de operación a realizar
        int NumCaja;

        //public caja() { }

        public void caja_proc(Object n) {

            this.NumCaja = (int)n;
            int[] c = { 1, 2, 3, 4, 5, 6, 7 }; //vector de clientes
            clientes = new Queue<int>(c);
            Random rOp = new Random();
            for (int i = 0; i < c.Length; i++)
            {
                this.op = rOp.Next(1, 4);
                this.SeleccionarOpcion();
            }
        }

        public void Pago() {

            Console.WriteLine("Caja #" + this.NumCaja + " atendiendo a cliente " + clientes.Peek()+" Pagando");
            Thread.Sleep(12000);
            clientes.Dequeue();
        }

        public void Retiro()
        {

            Console.WriteLine("Caja #" + this.NumCaja + " atendiendo a cliente " + clientes.Peek() +" Retirando");
            Thread.Sleep(8000);
            clientes.Dequeue();
        }

        public void Cosulta()
        {

            Console.WriteLine("Caja #" + this.NumCaja + " atendiendo a cliente " + clientes.Peek() + " Consultando saldo");
            Thread.Sleep(4000);
            clientes.Dequeue();
        }

        public void Transaccion()
        {

            Console.WriteLine("Caja #" + this.NumCaja + " atendiendo a cliente " + clientes.Peek() + " Haciendo transacción");
            Thread.Sleep(20000);
            clientes.Dequeue();
        }

        public void SeleccionarOpcion() {

             //genera una opción del 1 al 4;

            switch (this.op) { 
            
                case 1:
                    this.Pago();
                    break;
                case 2:
                    this.Retiro();
                    break;
                case 3:
                    this.Cosulta();
                    break;
                case 4:
                    this.Transaccion();
                    break;
            }
        
        }
    }
}


//---------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace simulacionCaja
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Clear();
            //caja2 c1 = new caja2();
            //c1.caja_proc(1);

            caja2 c1 = new caja2();
            caja2 c2 = new caja2();
            caja2 c3 = new caja2();
            caja2 c4 = new caja2();

            Thread h1 = new Thread(c1.caja_proc);
            Thread h2 = new Thread(c2.caja_proc);
            Thread h3 = new Thread(c3.caja_proc);
            Thread h4 = new Thread(c4.caja_proc);

            h1.Start(1);
            h2.Start(2);
            h3.Start(3);
            h4.Start(4);

            h1.Join();
            h2.Join();
            h3.Join();
            h4.Join();
            //Console.WriteLine("Borrando todo");
            //Thread.Sleep(1000);
            //Console.Clear();
            Console.ReadKey();
        }
    }
}

No hay comentarios:

Publicar un comentario