Sei sulla pagina 1di 7

package sistemasoperativos;

import java.applet.Applet;
import java.awt.*;
import java.util.LinkedList;
import javax.swing.*;
public class Cenando extends Applet implements Runnable
{
public final static int N = 5, pensando = 0, hambriento = 1,
comiendo = 2;
private static int estado[] = new int[N];
private static Semaforo mutex = new Semaforo(1);
private static Semaforo semaforos[] = new Semaforo[N];
int tmp2;
Label t1,t2,t3;
Label t5,t6,t7,t8,t9;
Thread filosofo1=null, filosofo2=null, filosofo3=null,
filosofo4=null, filosofo5=null;
@Override
public void init()
{
setBackground(Color.WHITE);
setSize(getMaximumSize());
t1=new Label("Pensando");
t1.setForeground(Color.DARK_GRAY);
add(t1);
t2=new Label("Hambriento");
t2.setForeground(Color.DARK_GRAY);
add(t2);
t3=new Label("Comiendo");
t3.setForeground(Color.DARK_GRAY);
add(t3);
t5=new Label();
t5.setForeground(Color.DARK_GRAY);
add(t5);
t6=new Label();
t6.setForeground(Color.DARK_GRAY);
add(t6);
t7=new Label();
t7.setForeground(Color.DARK_GRAY);
add(t7);
t8=new Label();
t8.setForeground(Color.DARK_GRAY);
add(t8);
t9=new Label();
t9.setForeground(Color.DARK_GRAY);
add(t9);
super.init();
}
public void graficos(Graphics g)
{
if(estado[0]==0)
{
g.setColor(Color.RED);
g.fillOval(200, 250, 100, 100);
}
else if(estado[0]==1)
{
g.setColor(Color.GREEN);
g.fillOval(200, 250, 100, 100);
}
else
{
g.setColor(Color.blue);
g.fillOval(200, 250, 100, 100);
}
if(estado[1]==0)
{
g.setColor(Color.RED);
g.fillOval(500, 37, 100, 100);
}
else if(estado[1]==1)
{
g.setColor(Color.GREEN);
g.fillOval(500, 37, 100, 100);
}
else
{
g.setColor(Color.blue);
g.fillOval(500, 37, 100, 100);
}
if(estado[2]==0)
{
g.setColor(Color.RED);
g.fillOval(780, 250, 100, 100);
}
else if(estado[2]==1)
{
g.setColor(Color.GREEN);
g.fillOval(780, 250, 100, 100);
}
else
{
g.setColor(Color.blue);
g.fillOval(780, 250, 100, 100);
}
if(estado[3]==0)
{
g.setColor(Color.RED);
g.fillOval(710, 550, 100, 100);
}
else if(estado[3]==1)
{
g.setColor(Color.GREEN);
g.fillOval(710, 550, 100, 100);
}
else
{
g.setColor(Color.blue);
g.fillOval(710, 550, 100, 100);
}
if(estado[4]==0)
{
g.setColor(Color.red);
g.fillOval(280, 560, 100, 100);
}
else if(estado[4]==1)
{
g.setColor(Color.GREEN);
g.fillOval(280, 560, 100, 100);
}
else
{
g.setColor(Color.blue);
g.fillOval(280, 560, 100, 100);
}
}
@Override
public void paint(Graphics g)
{
init();
g.setColor(Color.GRAY);
g.fillOval(290, 140, 500, 500);
g.setColor(Color.red);//rojo
g.fillOval(1000, 100, 40, 40);
t1.setBounds(1050, 100,80,40);
g.setColor(Color.GREEN);//verde
g.fillOval(1000, 150, 40, 40);
t2.setBounds(1050,150,80,40);
g.setColor(Color.blue);//azul
g.fillOval(1000, 200, 40, 40);
t3.setBounds(1050, 200,80,40);
t5.setBounds(1000, 400, 200, 50);
t6.setBounds(1000, 440, 200, 50);
t7.setBounds(1000, 480, 200, 50);
t8.setBounds(1000, 520, 250, 50);
t9.setBounds(1000, 560, 200, 50);
graficos(g);
super.paint(g);
}
// public void cena(int i)
// {
// tmp2=i;
// }
public void run()
{
int i=0;
// cena(tmp2);
i=tmp2;
if(tmp2<4)
tmp2++;
while(true)
{
think(i);
take_forks(i);
eat(i);
put_forks(i);
}
}
public void take_forks(int i)
{
mutex.down();
estado[i]=hambriento;
pintar();
test(i);
mutex.up();
semaforos[i].down();
}
public void put_forks(int i)
{
mutex.down();
estado[i]=pensando;
pintar();
int izq;
int der;
if(i==0)
{
izq=4;
}
else
{
izq=i-1;
}
if(i==4)
{
der=0;
}
else
{
der=i+1;
}
test(izq);
test(der);
mutex.up();
}
public void test(int i)
{
int izq;
int der;
if(i==0)
{
izq=4;
}
else
{
izq=i-1;
}
if(i==4)
{
der=0;
}
else
{
der=i+1;
}
if(estado[i]==hambriento&&estado[izq]!
=comiendo&&estado[der]!=comiendo)
{
estado[i]=comiendo;
pintar();
semaforos[i].up();
}
if(estado[i]==hambriento)
{
pintar();
}
}
public void think(int i)
{
pintar();
try
{
Thread.sleep(3000);
}
catch(InterruptedException e){}
}
public void eat(int i)
{
int x=-1;
while(x!=i)
{
x=(int)(Math.random()*10);
try
{
Thread.sleep(3000);
}
catch(InterruptedException e){}
}

}
public static void main(String mx[])
{

JFrame f=new JFrame("CENA DE FILOSOFOS");


Cenando obj=new Cenando();
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add("Center",obj);
for(int i=0; i<N; i++)
{
semaforos[i]=new Semaforo(0);
}
f.setSize(f.getMaximumSize());
f.show();
obj.orden();

}
void pintar()
{
try
{
repaint();
Thread.sleep(3000);
}
catch(Exception e){}
}
void orden()
{
String orden="";
LinkedList buffer = new LinkedList();
while (buffer.size()!=5)
{
int tmp= 1+ (int)(Math.random()*5);
if(!buffer.contains(tmp))
{
orden+=tmp;
buffer.add(tmp);
if(tmp==1)
{
filosofo1=new Thread(this,"Filosofo 1");
filosofo1.start();
}
if(tmp==2)
{
filosofo2=new Thread(this,"Filosofo 2");
filosofo2.start();
}
if(tmp==3)
{
filosofo3=new Thread(this,"Filosofo 3");
filosofo3.start();
}
if(tmp==4)
{
filosofo4=new Thread(this,"Filosofo 4");
filosofo4.start();
}
if(tmp==5)
{
filosofo5=new Thread(this,"Filosofo 5");
filosofo5.start();
}
}
}
System.out.println("ORDEN\n"+orden);
}
}

Potrebbero piacerti anche