}; //Customer data of sitting people. static void Main(string[] args) { //We create one barber, and couple of customers to simulate a barbershops daily flow. Barber barber = new Barber(); Customer customer0 = new Customer(); Customer customer1 = new Customer(); Customer customer2 = new Customer(); Customer customer3 = new Customer(); Customer customer4 = new Customer(); Customer customer5 = new Customer(); //We set the threads here. Thread barberThread = new Thread(new ThreadStart(Barber.cutHair)); Thread customerThread0 = new Thread(new ThreadStart(customer0.sitOnSeat)); Thread customerThread1 = new Thread(new ThreadStart(customer1.sitOnSeat)); Thread customerThread2 = new Thread(new ThreadStart(customer2.sitOnSeat)); Thread customerThread3 = new Thread(new ThreadStart(customer3.sitOnSeat)); Thread customerThread4 = new Thread(new ThreadStart(customer4.sitOnSeat)); Thread customerThread5 = new Thread(new ThreadStart(customer5.sitOnSeat)); //Threads fire up, as a barbershop needs a barber, barberThread is started first. barberThread.Start(); customerThread0.Start(); customerThread1.Start(); customerThread2.Start(); customerThread3.Start(); customerThread4.Start(); Thread.Sleep(500); //To simulate a empty barbershop after couple of customers, we make the last customer wait a while before coming in. customerThread5.Start(); Console.ReadKey(); }
static void Main(string[] args) { int numChairs = 4; // Create a channel with room for numChairs customers Channel <Customer> customers = new Channel <Customer>(JibuBuffer.Fifo, numChairs); // Start the barber task Async barber = new Barber(customers.ChannelReader).Start(); Random rand = new Random(); // Create 100 customers for (int i = 0; i < 100; i++) { Timer.SleepFor(rand.Next(300)); new Customer("Mr. " + i, customers.ChannelWriter).Start(); } // We poison the channel to shut the barber down. // Note that if there are any more customers // in the channel when it is poisoned, // the barber will cut them anyway. customers.Poison(); barber.WaitFor(); }
public void checkBarber(int customerIndex) { if (Barber.sleeping == true) //If the barber is sleeping { Console.WriteLine("Barber got woken up by {0}", customerID); Barber.customerIndex = customerIndex; Barber.sleeping = false; //Wakes up the barber areSeatsAccessable.Release(); //Lets other customers to sit down Barber.cutHair(); //Gets his haircut freeSeats.Release(); //Customer gives up his seat on waiting seats return; } areSeatsAccessable.Release(); //If barber isn't sleeping, lets other customers come to the shop. }
public Shop(Barber barber, int maxQueueSize) : base(new ThreadScheduler()) { _barber = barber; _maxQueueSize = maxQueueSize; Console.WriteLine("{0} is open.", this); OnStream().Of<Client>() .Where(c => !c.GotHaircut) .ReactWith(s => { Console.WriteLine("{0} entered the shop.", s.Payload); if (!_seatIsTaken) { SendToBarber(s.Payload); } else { SendToQueue(s.Payload); } }); OnStream().Of<Client>() .Where(c => c.GotHaircut) .ReactWith(s => { Console.WriteLine("{0} got haircut.", s.Payload); _seatIsTaken = false; if (_queue.Count == 0) { Console.WriteLine("[Barber] is sleeping..."); return; } var client = _queue.Dequeue(); SendToBarber(client); }); }
static void Main(string[] args) { int numChairs = 4; // Create a channel with room for numChairs customers Channel<Customer> customers = new Channel<Customer>(JibuBuffer.Fifo, numChairs); // Start the barber task Async barber = new Barber(customers.ChannelReader).Start(); Random rand = new Random(); // Create 100 customers for (int i = 0; i < 100; i++) { Timer.SleepFor(rand.Next(300)); new Customer("Mr. " + i, customers.ChannelWriter).Start(); } // We poison the channel to shut the barber down. // Note that if there are any more customers // in the channel when it is poisoned, // the barber will cut them anyway. customers.Poison(); barber.WaitFor(); }
public Customer(Barber barber) { this.barber = barber; }
public BarberShop(string name) { barber = new Barber(name); thread = new Thread(barber.Arrive); thread.Start(); }
// ReSharper disable UnusedParameter.Local static void Main(string[] args) // ReSharper restore UnusedParameter.Local { var barber = new Barber(); var shop = new Shop(barber, MaxQueueSize); var clients = Enumerable.Range(1, ClientsCount).Select(i => new Client(i)).ToList(); foreach (var client in clients) { Thread.Sleep(Random.Next(10, 200)); shop.Send(client); } Thread.Sleep(1000); var gotHaircut = clients.Count(c => c.GotHaircut); Console.WriteLine("{0} out of {1} clients have got a new haircut.", gotHaircut, ClientsCount); }
public BarberShop(int waitingRoomChairs) { waitingRoomSize = waitingRoomChairs; barber = new Barber("Chico", this); }