Advertisement
Jaydeep999997

Barber Shop - Semaphore

Oct 23rd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | Source Code | 0 0
  1. import java.util.concurrent.Semaphore;
  2. import java.util.Set;
  3. import java.util.HashSet;
  4. import java.util.Random;
  5.  
  6. class Demonstration {
  7.   public static void main(String[] args) throws Exception {
  8.     BarberShop.runTest();
  9.   }
  10. }
  11.  
  12. class BarberShop {
  13.   private static final Random random = new Random(System.currentTimeMillis());
  14.   private final Semaphore barberWaitingForCustomer = new Semaphore(0);
  15.   private final Semaphore customerWaitingToOcuupySeat = new Semaphore(0);
  16.   private final Semaphore customerWaitingForHairCutToFinish = new Semaphore(0);
  17.   private final Semaphore barberWaitingForCustomerToLeave = new Semaphore(0);
  18.   private final int maxWaitingSeats;
  19.   private int waitingCustomers = 0;
  20.  
  21.   public static void runTest() throws Exception {
  22.     BarberShop barberShop = new BarberShop(5);
  23.  
  24.     Thread barber =
  25.         new Thread(
  26.             new Runnable() {
  27.  
  28.               @Override
  29.               public void run() {
  30.                 try {
  31.                   barberShop.lifeCycleOfBarber();
  32.                 } catch (InterruptedException ie) {
  33.  
  34.                 }
  35.               }
  36.             });
  37.     barber.setName("Barber");
  38.     barber.start();
  39.  
  40.     Set<Thread> customers = new HashSet<Thread>();
  41.     for (int i = 0; i < 10; i++) {
  42.       Thread customer =
  43.           new Thread(
  44.               () -> {
  45.                 try {
  46.                   Thread.sleep(random.nextInt(100));
  47.                   barberShop.customerArrives();
  48.                 } catch (InterruptedException ie) {
  49.                 }
  50.               });
  51.       customer.setName("Customer_" + i);
  52.       customers.add(customer);
  53.     }
  54.  
  55.     for (Thread customer : customers) {
  56.       customer.start();
  57.     }
  58.  
  59.     for (Thread customer : customers) {
  60.       customer.join();
  61.     }
  62.     barber.join();
  63.   }
  64.  
  65.   public BarberShop(int maxWaitingSeats) {
  66.     this.maxWaitingSeats = maxWaitingSeats;
  67.   }
  68.  
  69.   public void customerArrives() throws InterruptedException {
  70.     synchronized (this) {
  71.       if (waitingCustomers == maxWaitingSeats) {
  72.         System.out.println(
  73.             "Not enough space to wait, " + Thread.currentThread().getName() + " is leaving shop.");
  74.         return;
  75.       }
  76.       waitingCustomers++;
  77.     }
  78.  
  79.     barberWaitingForCustomer.release();
  80.     System.out.println(Thread.currentThread().getName() + " has arrived.");
  81.  
  82.     customerWaitingToOcuupySeat.acquire();
  83.     System.out.println(Thread.currentThread().getName() + " has started getting hair-cut.");
  84.  
  85.     synchronized (this) {
  86.       waitingCustomers--;
  87.     }
  88.  
  89.     customerWaitingForHairCutToFinish.acquire();
  90.     System.out.println(Thread.currentThread().getName() + " has finished getting hair-cut.");
  91.  
  92.     barberWaitingForCustomerToLeave.release();
  93.     System.out.println(Thread.currentThread().getName() + " has left shop.");
  94.   }
  95.  
  96.   public void lifeCycleOfBarber() throws InterruptedException {
  97.     while (true) {
  98.       barberWaitingForCustomer.acquire();
  99.  
  100.       customerWaitingToOcuupySeat.release();
  101.  
  102.       System.out.println("Started giving hair cut...");
  103.       Thread.sleep(random.nextInt(150));
  104.       System.out.println("Done with hair cut.");
  105.  
  106.       customerWaitingForHairCutToFinish.release();
  107.  
  108.       barberWaitingForCustomerToLeave.acquire();
  109.     }
  110.   }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement