Tenho que resolver o problema do jantar dos filósofos usando Condition, Lock.
Quando executo o algoritmo ele está travando. Alguém poderia me da uma ajuda?
Segue o código, ainda tenho que retira o IF do método largaGarfo e utilizar o Condition, Lock, só que estou tento problemas no método acima.
package jantarFilosofico;
public class Garfo {
private int idGarfo;
private boolean estadoGarfo;
private int dono;
public Garfo(int id){
idGarfo = id;
estadoGarfo = false; // desocupado
dono = -1; // sem dono
}
public int getIdGarfo(){
return idGarfo;
}
public void setIdGarfo(int g){
idGarfo = g;
}
public int getDonoGarfo(){
return dono;
}
public void setDonoGarfo(int d){
dono = d;
}
public boolean getEstadoGarfo(){
return estadoGarfo;
}
public void setEstadoGarfo(boolean ocupado){
estadoGarfo = ocupado;
}
}
package filosofos;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class FilosofoGlutao implements Runnable{
private Lock lock = new ReentrantLock();
private Condition notFull = lock.newCondition();
private Condition notEmpty = lock.newCondition();
final int N = 5; // são cinco filosofos e cinco garfos...
List <Garfo> garfos; // garfos disponíveis 0, 1, 2, 3 e 4
int filosofo;
FilosofoGlutao (List <Garfo>gs, int fil){
garfos = gs;
filosofo = fil;
}
public void run(){
for (int i=0; i<5; i++){
// pensa ...
pensaMuito(filosofo);
// pega garfo da esquerda
try {
pegaGarfo(/*posiçao*/filosofo, /*dono*/filosofo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// pega garfo da direita
try {
pegaGarfo(/*posiçao*/(filosofo+1)%N, /*dono*/filosofo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// fatura o espaguete
comeEspaguete(filosofo);
// larga o garfo da esquerda
try {
largaGarfo(/*posiçao*/filosofo, /*dono*/filosofo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// larga o garfo da direita
try {
largaGarfo(/*posiçao*/(filosofo+1)%N, /*dono*/filosofo);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private void pensaMuito(int fil){
switch (fil) {
case 0: // filosofo 0 pensa por 1000 ms...
try{
System.out.println("!!>"+Thread.currentThread().getName()+" PENSA");
Thread.sleep(500);}
catch (InterruptedException e){}
case 1: // filosofo 1 pensa por 2000 ms...
try{
System.out.println("!!>"+Thread.currentThread().getName()+" PENSA");
Thread.sleep(1000);}
catch (InterruptedException e){}
case 2: // filosofo 1 pensa por 3000 ms...
try{
System.out.println("!!>"+Thread.currentThread().getName()+" PENSA");
Thread.sleep(1500);}
catch (InterruptedException e){}
case 3: // filosofo 1 pensa por 4000 ms...
try{
System.out.println("!!>"+Thread.currentThread().getName()+" PENSA");
Thread.sleep(2000);}
catch (InterruptedException e){}
case 4: // filosofo 1 pensa por 5000 ms...
try{
System.out.println("!!>"+Thread.currentThread().getName()+" PENSA");
Thread.sleep(2500);}
catch (InterruptedException e){}
}
}
private void pegaGarfo(int pos, int dono) throws InterruptedException{
lock.lock();
try{
while(((Garfo)garfos.get(pos)).getEstadoGarfo()){ // ocupado
notFull.await(); // espera até que não esteja ocupado
}
System.out.println("++>"+Thread.currentThread().getName()+" PEGA GARFO "+ pos);
((Garfo)garfos.get(pos)).setEstadoGarfo(true); // pega garfo
((Garfo)garfos.get(pos)).setDonoGarfo(dono); // pega garfo
notEmpty.signal(); //avisa threads esperando para ler do buffer
}catch(InterruptedException e){e.printStackTrace();}
finally{lock.unlock();} //libera acesso ao buffer
}
private void largaGarfo(int pos, int dono) throws InterruptedException{
if (((Garfo)garfos.get(pos)).getEstadoGarfo()==true &&
((Garfo)garfos.get(pos)).getDonoGarfo() == dono){ // desocupado
System.out.println("-->"+Thread.currentThread().getName()+" LARGA GARFO "+ pos);
((Garfo)garfos.get(pos)).setEstadoGarfo(false); // garfo liberado
((Garfo)garfos.get(pos)).setDonoGarfo(-1); // garfo sem dono
}
}
private void comeEspaguete(int fil){
// se ambos os garfos estiverem reservados pelo
// filosofo "fil", então ele come espaguete...
// Testar a sua solução de proteção, comente o if, deixando apenas o
// seu conteúdo liberado
if (((Garfo)garfos.get(fil)).getEstadoGarfo() &&
((Garfo)garfos.get((fil+1)%N)).getEstadoGarfo() &&
((Garfo)garfos.get(fil)).getDonoGarfo()==fil &&
((Garfo)garfos.get((fil+1)%N)).getDonoGarfo()==fil){
System.out.println("@@>"+Thread.currentThread().getName()+" COME ESPAGUETE");
try{ Thread.sleep(5000);}
catch (InterruptedException e){}
}
}
}
package filosofos;
import java.util.List;
import java.util.ArrayList;
public class ProblemaDosFilosofosGlutoes{
public static void main(String args[]){
// cria os grafos (coleção de 5 garfos)
List<Garfo>garfos = new ArrayList<Garfo>();
for (int i = 0; i<=4; i++){
Garfo garfo = new Garfo(i);
garfos.add(i,garfo);
}
// cria a thread do filosofo 0
FilosofoGlutao r0 = new FilosofoGlutao(garfos, 0);
Thread f0 = new Thread(r0);
// cria a thread do filosofo 1
FilosofoGlutao r1 = new FilosofoGlutao(garfos, 1);
Thread f1 = new Thread(r1);
// cria a thread do filosofo 2
FilosofoGlutao r2 = new FilosofoGlutao(garfos, 2);
Thread f2 = new Thread(r2);
// cria a thread do filosofo 3
FilosofoGlutao r3 = new FilosofoGlutao(garfos, 3);
Thread f3 = new Thread(r3);
// cria a thread do filosofo 4
FilosofoGlutao r4 = new FilosofoGlutao(garfos, 4);
Thread f4 = new Thread(r4);
// nomeia as threads
f0.setName("F0");
f1.setName("F1");
f2.setName("F2");
f3.setName("F3");
f4.setName("F4");
// manda as threads pra fila de pronto
f0.start();
f1.start();
f2.start();
f3.start();
f4.start();
}
}
Obg

