🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Ayuda programando concurrente

#28055
From: mcm
Date: Tue, 02 Jun 1998 00:00
56 lines
1715 bytes
Hay un problema en este programa:

Al cabo de un tiempode 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.

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 synchoronized 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]);}
}

------------------------------------------------
Technical Details
Message-ID: <6l422i$sgc$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,bc5b8bab3abb1a43,start
X-Google-Attributes: gid1116f3,public
From: "mcm" <mcm@ferca.net>
Subject: Ayuda programando concurrente
Date: 1998/06/02
Message-ID: <6l422i$sgc$1@talia.mad.ibernet.es>
X-Deja-AN: 359237790
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4
Organization: Telefonica Transmision de Datos
Newsgroups: es.comp.lenguajes.java