/*****************************************************************
- Memorial University of Newfoundland<br>
- 8893 Concurrent Programming<br>
- Sleeping Barber Problem
- @author Dennis Peters
-
@version 2000.02.02
****************************************************************/
public class Sleepytown {
public static void main(String[] args) {
try {
Barbershop sh = new Barbershop();
Thread barber = new Thread(new Barber(sh));
Thread c = null;
for (int i = 1; i < 10; i++) {
c = new Thread(new Customer(i, sh));
c.start();
}
barber.start();
int count = 0;
while (barber.isAlive() && count < 20) {
count++;
Thread.sleep(5000);
}
}
catch (InterruptedException e) { }
System.out.println("Barbershop done");
System.exit(0);
}
}
/*****************************************************************
- Model of the Barbershop.
-
- Invariant: inChair = 0 <-> chair is empty /\
-
inChair = x <-> customer x is sitting in chair /\
-
barberReady <-> barber is available to cut hair /\
-
doorOpen <-> haircut finished</pre>
****************************************************************/
class Barbershop {
int inChair; // Customer who is currently in chair. 0 = nobody
boolean barberReady; // used to signal that barber is ready for customer
boolean doorOpen; // used to signal that door is open
Barbershop()
{
inChair = 0;
barberReady = false;
doorOpen = false;
}
/*****************************************************************
- Called by customer to get a haircut.
****************************************************************/
public synchronized void getHaircut(int id)
throws InterruptedException
{
while (!barberReady || inChair != 0) wait();
inChair = id; // Sit in the chair
System.out.println(id + " in chair.");
notifyAll();
while (!doorOpen) wait();
inChair = 0;
System.out.println(id + " leaves.");
notifyAll();
}
/*****************************************************************
- Called by Barber to start cutting hair.
****************************************************************/
public synchronized int getNextCustomer()
throws InterruptedException
{
barberReady = true;
notifyAll();
while (inChair == 0) wait();
barberReady = false;
notifyAll();
return inChair;
}
/*****************************************************************
- Called by Barber to finish cutting hair.
****************************************************************/
public synchronized void finishedCut(int id)
throws InterruptedException
{
doorOpen = true;
notifyAll();
while (inChair != 0) wait();
doorOpen = false;
notifyAll();
}
}
/*****************************************************************
- Customers have fast growing hair.
****************************************************************/
class Customer implements Runnable {
int myId; // each customer knows his/her own Id
Barbershop theOnlyShopInTown; // Where do I get my hair cut
Customer(int id, Barbershop s)
{
myId = id;
theOnlyShopInTown = s;
}
public void run()
{
try {
while (true) {
Thread.sleep(Math.round(Math.random()<em>myId</em>100)); // Let my hair grow
System.out.println(myId + " is scruffy.");
theOnlyShopInTown.getHaircut(myId);
System.out.println(myId + " is kempt.");
}
}
catch (InterruptedException e) { } // Simply exit.
}
}
/*****************************************************************
- Barber cuts hair when he can
****************************************************************/
class Barber implements Runnable {
Barbershop myWorkplace;
Barber(Barbershop s)
{
myWorkplace = s;
}
public void run()
{
try {
int customer;
while (true) {
System.out.println(“Barber looking for next customer.”);
customer = myWorkplace.getNextCustomer();
System.out.println(“Barber is cutting " + customer + “'s hair.”);
Thread.sleep(Math.round(Math.random()*1000)); // Work for some time
System.out.println(” … done " + customer);
myWorkplace.finishedCut(customer);
}
}
catch (InterruptedException e) { } // Simply exit.
}
}
pq apesar de o programa usar threads e
variaveis compartilhadas, ele funciona normalmente,
sem entrar em condições de disputa…alguem ajuda ae…naum entendi…valew…