User Tools

Site Tools


lab7

This is an old revision of the document!


Lab 7

You are asked to implement a solution to the sleeping barber problem in Java. Some Java code has already been developed (see below). First, we describe the problem.

The barber has one barber's chair and a waiting room containing a number of chairs in it. When the barber finishes cutting a customer's hair, he dismisses the customer and goes to the waiting room to see if there are others waiting. If there are, he brings one of them back to the chairs and cut their hair. If there are none, he returns to the chair and sleeps in it.

Each customer, when they arrive, looks to see what the barber is doing. If the barber is sleeping, the customer wakes him up. If the barber is cutting hair, the customer stays in the waiting room. If there is a free chair in the waiting room, the customer sits in it and waits their turn. If there is no free chair, the customer leaves.

The waiting room is represented by the class WaitingRoom and the barber and the customers are represented by Barber and Customer threads. The API of these classes can be found <a href=“barber.a pi/index.html”>here</a>. Implement the classes WaitingRoom and Barber. An implementation of the Customer class can be found here. The sleeping barber app can be found <a href=“Main.java.txt”>here</a>.

Since randomization is used in some of the classes, different runs of the app may produce different outputs.

Barber sleeps
Customer 4 enters
Customer 4 sits down
Barber wakes up
Customer 4 gets a haircut
Customer 4 leaves
Customer 3 enters
Customer 3 sits down
Customer 1 enters
Customer 1 sits down
Customer 3 gets a haircut
Customer 3 leaves
Customer 0 enters
Customer 0 sits down
Customer 2 enters
Customer 2 sits down
Customer 1 gets a haircut
Customer 1 leaves
Customer 0 gets a haircut
Customer 0 leaves
Customer 2 gets a haircut
Customer 2 leaves
Barber is done for the day

In the above run, there are never more than three customers in the waiting room and therefore all customers get a haircut.

Barber sleeps
Customer 1 enters
Customer 1 sits down
Barber wakes up
Customer 2 enters
Customer 2 sits down
Customer 3 enters
Customer 3 sits down
Customer 4 enters
Customer 4 leaves because no chair is free
Customer 0 enters
Customer 0 leaves because no chair is free
Customer 1 gets a haircut
Customer 1 leaves
Customer 2 gets a haircut
Customer 2 leaves
Customer 3 gets a haircut
Customer 3 leaves
Barber is done for the day

In the above run, customers 4 and 0 leave before getting a haircut since all chairs in the waiting room are occupied.

To receive feedback, submit your code using the submit command:

submit 4315 lab7 <name of class>.java
lab7.1551922173.txt.gz · Last modified: 2019/03/07 01:29 by franck