Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Deliverable required: 1. Please implement the barber shop for one barber and 5 c

ID: 3677070 • Letter: D

Question

Deliverable required:

1. Please implement the barber shop for one barber and 5 chairs in the waiting room of the barber shop.

2. The barber shop should be implemented for 20 customers.

3. The Customers should come into the barber shop in fixed intervals.

4. Please provide appropriate output as follows:

a. Customer (i) enters barber shop

b. Customer (i) waiting in waiting room

c. Barber(i) cutting hair

d. Customer(i) getting hair cut.

e. No more chairs in waiting room, Customer(i) leaves shop. customers have to leave the shop since there are no more chairs (approximately half the customers have to leave the shop).

5.Adjust the customer entry interval and the barber cutting times such that around 10 of the customers have to leave the shop

since there are no more chairs (approximately half the customers have to leave the shop

6. Print the Hair Cutting time and Customer entry intervals.

7. Now re-implement the solution (Steps 1-6) with 2 barbers, with the same parameters as

Please provide outputs for both the 1 barber and 2 barber solutions, and the source code for both the

instances. Source code that you could start with is provided

above and show the output – with 2 barbers, more customers should get their haircuts.

Please provide outputs for both the 1 barber and 2 barber solutions, and the source code for both the

instances. Source code that you could start with is provided below:

_____________________________________________________-

Explanation / Answer

package com.concurrency;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SleepingBarberUsingReentrantLock extends Thread
{

public static final int CHAIRS = 5;


public static int numberOfFreeSeats = CHAIRS;

public static CustomerPool customers = new CustomerPool(CHAIRS);

public static Lock barber = new ReentrantLock();
public static Condition barberAvailable = barber.newCondition();

public static Lock accessSeats = new ReentrantLock();
class Customer extends Thread {

int id;
boolean notCut = true;

public Customer(int i)
{
   id = i;
}

public void run()
{
   while (notCut)
   {
    accessSeats.lock();
    if (numberOfFreeSeats > 0)
    {
     System.out.println("Customer " + this.id + " just sat down.");
     numberOfFreeSeats--;
     customers.releaseCustomer();
        
     accessSeats.unlock();
     barber.lock();
     try
     {
      barberAvailable.await();
     }
     catch (InterruptedException e)
     {
     }
     finally
     {
      barber.unlock();
     }
     notCut = false;
     this.get_haircut();
    }
    else
    {
     System.out.println("There are no free seats. Customer " + this.id + " has left the barbershop.");
     accessSeats.unlock();
     notCut = false;
    }
   }
}


public void get_haircut()
{
   System.out.println("Customer " + this.id + " is getting his hair cut");
   try {
    sleep(5050);
   }
   catch (InterruptedException ex)
   {
   }
}

}

class Barber extends Thread
{

public Barber()
{
}

public void run()
{
   while (true)
   {
    try
    {
     customers.acquireCustomer();
     accessSeats.lock();
     numberOfFreeSeats++;
     barber.lock();
     try {
      barberAvailable.signal();
     } finally {
      barber.unlock();
     }
     accessSeats.unlock();
     this.cutHair();
    }
    catch (InterruptedException ex)
    {
    }
   }
}


public void cutHair()
{
   System.out.println("The barber is cutting hair");
   try {
    sleep(5000);
   } catch (InterruptedException ex)
   {
   }
}
}

public static void main(String args[])
{

SleepingBarberUsingReentrantLock barberShop = new SleepingBarberUsingReentrantLock();
barberShop.start();
}

public void run()
{
Barber barber = new Barber();
barber.start();

for (int i = 1; i < 16; i++)
{
   Customer aCustomer = new Customer(i);
   aCustomer.start();
   try {
    sleep(2000);
   } catch (InterruptedException ex)
   {
   }
}
}
}

class CustomerPool {

    private final Lock lock = new ReentrantLock();
    private final Condition poolAvailable = lock.newCondition();
    private int num_customers;
    private final int max_num_customers;


    public CustomerPool(int num_customer_pools) {
            this.max_num_customers = num_customer_pools;
            this.num_customers = 0;
    }

    public void acquireCustomer() throws InterruptedException
    {
        lock.lock();
        try {
            while (num_customers <= 0)
                poolAvailable.await();
            --num_customers;
        } finally {
            lock.unlock();
        }
    }

    public void releaseCustomer()
    {
        lock.lock();
        try
        {
          if(num_customers >= max_num_customers)    
                return;
            ++num_customers;
            poolAvailable.signal();
        } finally {
            lock.unlock();
        }
    }
  
    public int getNumOfCustomers()
    {
     return num_customers;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote