
// BarberShop.java
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* The Sleeping barber problem:
  https://en.wikipedia.org/wiki/Sleeping_barber_problem

  This is a version of the problem with multiple barbers, 
  and a fixed-size waiting room with alerts and a 
  queue of customers.

  Customers arrive at random times and if there is space 
  in the waiting room then an alert is left for a barber. 
  If the room is full, the customer leaves, but will return
  after some random time.

  When a barber finishes with a customer, he checks the waiting 
  room for alerts, and begins cutting the hair of the next 
  customer in the queue. If there are no alerts, he goes to 
  sleep until woken by an alert.

  Usage:
    > compile.bat BarberShop.java
    > run.bat BarberShop

    Part of the application made up of:
       * BarberShop.java
       * Barber.java
       * WaitingRoom.java
       * Customer.java
*/


import org.jspace.*;


public class BarberShop
{
  private static final int NUM_BARBERS = 2;
  private static final int NUM_CUSTS = 7;
  private static final int ROOM_SIZE = 3;


  public static void main(String[] args)
                             throws InterruptedException
  {
    Space space = new SequentialSpace();
    // Space space = new VisSpace(500);   
                    // faster than the default

    PrintAreas pas = new PrintAreas(NUM_BARBERS+NUM_CUSTS);   

    WaitingRoom room = new WaitingRoom(ROOM_SIZE, space);  

    System.out.println("Create " + NUM_BARBERS + " barbers");
    for(int i=0; i < NUM_BARBERS; i++)
      new Thread( new Barber(i, room, pas, space),
                                   "Barber-"+i).start();

    System.out.println("Create " + NUM_CUSTS + " customers");
    for(int i=0; i < NUM_CUSTS; i++) {
      new Thread( new Customer(i, room, pas, space),
                                     "Cust-"+i).start();
    }
  } /* main */


}  // end of BarberShop class
