User Tools

Site Tools


sleeping-barber

Another classical problem takes place in a barber shop. This problem is due to Dijkstra. The barber shop has one barber, a barber chair, and n chairs for waiting customers, if any, to sit on. If there are no customers present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while the barber is cutting a customer's hair, then they sit down (if there are empty chairs) or leave the shop (if all chairs are full). The problem is how to program the barber and the customers without getting into race conditions. In your solution, you may want to introduce a class Shop. The Shop object has-a Barber object. A Barber object is-a Thread object. The run method of the Barber class should give rise to the output The barber sits down and falls asleep and The barber wakes up, whenever appropriate. In your solution, you may also want to introduce a class Customer. A Customer object is-a Thread. The run method of the Customer class should give rise to the output Customer i enters, Customer i sits down (if there is an empty chair), Customer i gets a haircut (if there is an empty chair and once it is his turn), and Customer i leaves.

An applet of this problem can be found here.

public class Barber extends Thread 
{
  public void run()
  {
    while (true)
    {
      while (shop.isEmpty())
      {
        try
        {
          this.wait();
        }
        catch (InterruptedException e) {}
      }
    }
  }
}
public class Customer extends Thread 
{
  private int id;
  private Shop shop;
 
  public Customer(int id)
  {
    super();
    this.id = id;
  }
 
  public void run()
  {
    while (true)
    {
      System.out.println("Customer " + this.id + " enters");
      int number = shop.getChair();
      if (number != -1)
      {
        System.out.println("Customer " + this.id + " sits down");
        while (!shop.isFirst(number))
        {
          try
          {
            this.wait();
          }
          catch (InterruptedException e) {}
        }
        System.out.println("Customer " + this.id + " gets a haircut");
        while (!shop.isDone())
        {
          try
          {
            this.wait();
          }
          catch (InterruptedException e) {}
        }
      }
      System.out.println("Customer " + this.id + " leaves");
    }
  }
}
public class Shop 
{
  private Barber barber;
 
  public final int NUMBER_OF_CHAIRS = 8;
 
  private int first;
  private int next;
  private boolean done;
 
  public synchronized int getChair()
  {
    if (this.next - this.first > Shop.NUMBER_OF_CHAIRS)
    {
      return -1;
    }
    else
    {
      this.notifyAll();
      return last++;
    }
  }
 
  public synchronized boolean isFirst(int number)
  {
    return this.first == number;
  }
 
  public synchronized boolean isDone()
  {
    return this.done;
  }	
}

Another sketch of a solution can be found here.

Slawomir's running (!!!) solution can be found here.

sleeping-barber.txt · Last modified: 2007/10/29 20:51 by cs253184

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki