Tareas
/*Clase Principal del programa*/
public class Filosofos{
public static void main( String args[] )
{
Tenedor tenedor[]=new Tenedor[5];
/*Instancias delos 5 tenedores */
tenedor[0]=new Tenedor(0);
tenedor[1]=new Tenedor(1);
tenedor[2]=new Tenedor(2);
tenedor[3]=new Tenedor(3);
tenedor[4]=new Tenedor(4);
Filosofo filosofo[]=new Filosofo[5];/*instancias de los cinco filosofos*/
filosofo[0]=new Filosofo(0,tenedor[0],tenedor[1]);
filosofo[1]=new Filosofo(1,tenedor[1],tenedor[2]);
filosofo[2]=new Filosofo(2,tenedor[2],tenedor[3]);filosofo[3]=new Filosofo(3,tenedor[3],tenedor[4]);
filosofo[4]=new Filosofo(4,tenedor[4],tenedor[0]);
/*Comienza la ejecucion de los filosofos*/
filosofo[0].start();
filosofo[1].start();filosofo[2].start();
filosofo[3].start();
filosofo[4].start();
}
}
package concurrente.filosofos;
package concurrente.filosofos;
import java.lang.*;
import java.util.Random;
public class Filosofoextends Thread {
private int identificativo;
private Tenedor izquierda;
private Tenedor derecha;
private Random random;
private boolean izquierdatomado;
private boolean derechatomado;
privateint elegido;
private boolean flag;
Filosofo(int identificativo_, Tenedor izquierda_, Tenedor derecha_)
{
identificativo=identificativo_;
izquierda=izquierda_;
derecha=derecha_;
random = newRandom();
}
public void pensar()
{
try
{
this.sleep(10000);
}catch(InterruptedException ie)
{
}
}
public void comer()
{
try
{
System.out.println("comiendo...");System.out.println(identificativo);
this.sleep(10000);
System.out.println("terminando de comer...");
System.out.println(identificativo);
}catch(InterruptedException ie)
{
}
}
public void run()
{while(true)
{
this.pensar();
flag=false;
elegido=random.nextInt(2);
if(elegido==0)
{
if(!izquierda.esocupado())
{
izquierda.tomar();
izquierdatomado=true;
}
else if(!derecha.esocupado())
{...
Regístrate para leer el documento completo.