import static java.util.concurrent.TimeUnit.SECONDS; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.Scanner; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; public class SleepingBarber { public static void main (String a[]) throws InterruptedException { int noOfBarbers=2, customerId=1, noOfCustomers=100, noOfChairs; //initializing the number of barber and customers Scanner sc = new Scanner(System.in); System.out.println("Enter the number of barbers(M):"); //input M barbers noOfBarbers=sc.nextInt(); System.out.println("Enter the number of waiting room" //input N waiting chairs + " chairs(N):"); noOfChairs=sc.nextInt(); // System.out.println("Enter the number of customers:"); //inout the number of customers for the shop // noOfCustomers=sc.nextInt(); ExecutorService exec = Executors.newFixedThreadPool(12); //initializing with 12 threads as multiple of cores in the CPU, here 6 Bshop shop = new Bshop(noOfBarbers, noOfChairs); //initializing the barber shop with the number of barbers Random r = new Random(); //a random number to calculate delays for customer arrivals and haircut System.out.println("\nBarber shop opened with " +noOfBarbers+" barber(s)\n"); long startTime = System.currentTimeMillis(); //start time of program for(int i=1; i<=noOfBarbers;i++) { //generating the specified number of threads for barber Barber barber = new Barber(shop, i); Thread thbarber = new Thread(barber); exec.execute(thbarber); } for(int i=0;i listCustomer; Random r = new Random(); public Bshop(int noOfBarbers, int noOfChairs){ this.nchair = noOfChairs; //number of chairs in the waiting room listCustomer = new LinkedList(); //list to store the arriving customers this.noOfBarbers = noOfBarbers; //initializing the the total number of barbers availableBarbers = noOfBarbers; } public AtomicInteger getTotalHairCuts() { totalHairCuts.get(); return totalHairCuts; } public AtomicInteger getCustomerLost() { customersLost.get(); return customersLost; } public void cutHair(int barberId) { Customer customer; synchronized (listCustomer) { //listCustomer is a shared resource so it has been synchronized to avoid any //unexpected errors in the list when multiple threads access it while(listCustomer.size()==0) { System.out.println("\nBarber "+barberId+" is waiting " + "for the customer and sleeps in his chair"); try { listCustomer.wait(); //barber sleeps if there are no customers in the shop } catch(InterruptedException iex) { iex.printStackTrace(); } } customer = (Customer)((LinkedList)listCustomer).poll(); //takes the first customer from the head of the list for haircut System.out.println("Customer "+customer.getCustomerId()+ " finds the barber asleep and wakes up " + "the barber "+barberId); } int millisDelay=0; try { availableBarbers--; //decreases the count of the available barbers as one of them starts //cutting hair of the customer and the customer sleeps System.out.println("Barber "+barberId+" cutting hair of "+ customer.getCustomerId()+ " so customer sleeps"); double val = r.nextGaussian() * 2000 + 4000; //time taken to cut the customer's hair has a mean of 4000 milliseconds and millisDelay = Math.abs((int) Math.round(val)); //and standard deviation of 2000 milliseconds Thread.sleep(millisDelay); System.out.println("\nCompleted Cutting hair of "+ customer.getCustomerId()+" by barber " + barberId +" in "+millisDelay+ " milliseconds."); totalHairCuts.incrementAndGet(); //exits through the door if(listCustomer.size()>0) { System.out.println("Barber "+barberId+ //barber finds a sleeping customer in the waiting room, wakes him up and " wakes up a customer in the " //and then goes to his chair and sleeps until a customer arrives + "waiting room"); } availableBarbers++; //barber is available for haircut for the next customer } catch(InterruptedException iex) { iex.printStackTrace(); } } public void add(Customer customer) { System.out.println("\nCustomer "+customer.getCustomerId()+ " enters through the entrance door in the the shop at " +customer.getInTime()); synchronized (listCustomer) { if(listCustomer.size() == nchair) { //No chairs are available for the customer so the customer leaves the shop System.out.println("\nNo chair available " + "for customer "+customer.getCustomerId()+ " so customer leaves the shop"); customersLost.incrementAndGet(); return; } else if (availableBarbers > 0) { //If barber is available then the customer wakes up the barber and sits in //the chair ((LinkedList)listCustomer).offer(customer); listCustomer.notify(); } else { //If barbers are busy and there are chairs in the waiting room then the customer //sits on the chair in the waiting room ((LinkedList)listCustomer).offer(customer); System.out.println("All barber(s) are busy so "+ customer.getCustomerId()+ " takes a chair in the waiting room"); if(listCustomer.size()==1) listCustomer.notify(); } } } }