Filosofos En Java

Páginas: 2 (393 palabras) Publicado: 10 de abril de 2012
import java.lang.*;
import java.util.Random;

/* Clase Tenedor */

class Tenedor
{
private boolean ocupado=false;
private int identificativo;

Tenedor(int identificativo_){
identificativo = identificativo_;
}

/*Accion de soltar tenedor*/
synchronized void dejar()
{
ocupado=false;
notify();
}

/*Comprobar si el tenedoresta ocupado*/
synchronized boolean esocupado()
{
if(ocupado)
{
return true;
}
else
{
return false;
}
}
/*Accion de tomar eltenedor*/
synchronized void tomar()
{
ocupado=true;
}

void soltar()
{
ocupado=false;
}
}

class Filosofo extends Thread
{
private Stringidentificativo;
private Tenedor izquierda;
private Tenedor derecha;
private Random random;
private boolean izquierdatomado;
private boolean derechatomado;
private int elegido;
privateboolean flag;

Filosofo(String identificativo_, Tenedor izquierda_, Tenedor derecha_)
{
identificativo=identificativo_;
izquierda=izquierda_;
derecha=derecha_;random = new Random();
}

public void pensar()
{
try
{
System.out.println(identificativo+" ESTA PENSANDO........................");
this.sleep(10000);System.out.println(identificativo+" YA NO PIENSA TIENE HAMBRE..... ***********************");
}
catch(InterruptedException ie)
{}
}

public void comer()
{
try
{System.out.println(identificativo+" ESTA COMIENDO.....+++++++++++++++++++++");
this.sleep(10000);
System.out.println(identificativo+" A TERMINADO DECOMER...!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
}
catch(InterruptedException ie)
{}
}

public void run()
{
while(true)
{
this.pensar();
flag=false;
elegido=random.nextInt(2);...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • La cena de los filosofos JAVA
  • Java
  • Java
  • Java
  • java
  • JAVA
  • java
  • java

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS