Esse código aki ..o q acontece?

2 respostas
M

/*****************************************************************

  • 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 &lt; 10; i++) {
    c = new Thread(new Customer(i, sh));
    c.start();
  }
  barber.start();
  int count = 0;
  while (barber.isAlive() &amp;&amp; count &lt; 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 &lt;-&gt; customer x is sitting in chair /\
    
  • barberReady &lt;-&gt; barber is available to cut hair /\
    
  • doorOpen &lt;-&gt; 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…

2 Respostas

P

simples ele sincroniza aos metodos, veja que na declaração dos metodos esta da seguinte forma public synchronized, e quando ele que liberar o metodo ele chama o metodo notifyall()

R

Se você olhar um livro de sistemas operacionais ou de computação paralela, processos, thread, você vai ver que é uma resolução para o problema do barbeiro.

Criado 20 de abril de 2007
Ultima resposta 20 de abr. de 2007
Respostas 2
Participantes 3