🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.java
Article #28140

Ayuda programa "filosofos comedore de espagetti"

#28140
From: mcm
Date: Mon, 08 Jun 1998 00:00
103 lines
2974 bytes
Hay un problema en este programa:

Al cabo de un tiempo de  bloquean los 5 fil�sofos con su tenedor derecho en
la mano.
�Qu� hay que variar en el programa para garantizar que no se interbloqueen
los fil�sofos?.
Yo no encuentro el problema.

Compilar:javac filisofos.java
Ejecutar: java Congreso

PROGRAMA:
----------------------------------------------------------------------
abstract class Sintaxis {
protected static final void WAIT(SemaforoBinario s){ s.P(); }
protected static final void SIGNAL(SemaforoBinario s){ s.V(); }
}

class SemaforoBinario {
private int valor=0;

  public SemaforoBinario(int cuenta) {
    valor=(cuenta!=0) ? 1:0;
}

  public synchronized void P() {
    valor--;
    while (valor<0) {
       try { wait();
          } catch (InterruptedException e) {}
     }
}

  public synchronized void V() {
   valor++;
   if (valor<=0)notify();
   if (valor>1) valor=1;
  }
}

class Mesa extends Sintaxis {
  private SemaforoBinario[] tenedor=null;

  public Mesa() {
     tenedor=new SemaforoBinario[5];
     for (int i=0;i<5;i++){
      tenedor[i]=new SemaforoBinario(1);
   }
}

public void tomaTenedor(int i) {WAIT(tenedor[i]);}

public void dejaTenedor(int i) {SIGNAL(tenedor[i]);}
}

class Filosofo extends Thread {
  private Mesa mesa;
  private int numero, derecho, izquierdo;

public Filosofo(int n, Mesa m) {
  mesa=m;
  numero=n;
  derecho=numero;
  izquierdo=(numero+1)%5;
}

public void run() {
 while (true) {
    System.out.println("Fil."+numero+"Pensando");
     try { sleep((int)(Math.random()*100));
        } catch (InterruptedException e) {}
     mesa.tomaTenedor(derecho);
     System.out.println("Fil."+numero+"con Tenedor"+derecho);
     try { sleep((int)(Math.random()*10));
        } catch (InterruptedException e) {}
     mesa.tomaTenedor(izquierdo);
    System.out.println("Fil."+numero+"Comiendo");
     try { sleep((int)(Math.random()*100));
        } catch (InterruptedException e) {}
     mesa.dejaTenedor(izquierdo);
     mesa.dejaTenedor(derecho);
    }
  }
}

class Congreso {
  public static void main (String args[]){
    Mesa mesa=new Mesa();
    Filosofo f0=new Filosofo(0,mesa);
    Filosofo f1=new Filosofo(1,mesa);
    Filosofo f2=new Filosofo(2,mesa);
    Filosofo f3=new Filosofo(3,mesa);
    Filosofo f4=new Filosofo(4,mesa);
    f0.start();
    f1.start();
    f2.start();
    f3.start();
    f4.start();
  }
}

----------------------------------------------
Technical Details
Message-ID: <6lhpfi$fej$1@talia.mad.ibernet.es>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: 1116f3,2b98c3c8a2bc46de,start
X-Google-Attributes: gid1116f3,public
From: "mcm" <mcm@ferca.net>
Subject: Ayuda programa "filosofos comedore de espagetti"
Date: 1998/06/08
Message-ID: <6lhpfi$fej$1@talia.mad.ibernet.es>
X-Deja-AN: 360831676
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
Organization: Telefonica Transmision de Datos
Newsgroups: es.comp.lenguajes.java