Galera é o seguinte, tenho as seguintes classes, porém depois de um tempo aparentemente ficam todas as threads suspensas e me parece que so a relativa ao filosofo 4 fica ativa, sendo assim ela produz somente resultados relativos a esse filósofo.
class FilosofoGlutao implements Runnable{
boolean cond = false;
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);
try{
// pega garfo da esquerda
pegaGarfo(/*posiçao*/filosofo, /*dono*/filosofo);
// pega garfo da direita
pegaGarfo(/*posiçao*/(filosofo+1)%N, /*dono*/filosofo);
// fatura o espaguete
comeEspaguete(filosofo);
// larga o garfo da esquerda
largaGarfo(/*posiçao*/filosofo, /*dono*/filosofo);
// larga o garfo da direita
largaGarfo(/*posiçao*/(filosofo+1)%N, /*dono*/filosofo);
}catch (InterruptedException e) {
System.out.print("Processamento da impressora interrompido.");
}
}
}
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 synchronized void pegaGarfo(int pos, int dono) {
while(((Garfo)garfos.get(pos)).getEstadoGarfo()){
try{
wait();
}catch (InterruptedException e) { }
}
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
notifyAll();
//}
}
private synchronized void largaGarfo(int pos, int dono) throws InterruptedException{
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){}
}
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();
}