Filosofos En Java
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);...
Regístrate para leer el documento completo.