C:\Documents and Settings\01031010262\Desktop\Apresentação_FINAL\Codigo2\DinningPhilsophers\src\Philosopher.java |
package DiningPhilosophers;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class Philosopher extends JLabel implements Runnable
{
private static final long serialVersionUID = 9144127262487093715L;
private String status;
private int id;
private boolean c;
private Thread t;
private String name;
private DinningTable table;
private static Icon icone[] = {new ImageIcon("./images/thinking.jpg"),
new ImageIcon("./images/hungry.jpg"),
new ImageIcon("./images/eating.jpg")};
@SuppressWarnings("static-access")
public Philosopher(DinningTable table, int i, String name)
{
super(icone[0]);
c = true;
id = i;
this.table = table;
this.name = name;
this.setToolTipText(name);
}
public void run()
{
while(c)
{
this.Think();
table.getForks(id);
Eat();
table.releaseForks(id);
}
}
public String getStatus() {return status;}
public void setStatus(String status) {this.status = status;}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public void AnThink()
{
this.status = "THINKING";
this.setToolTipText(name +" "+status);
this.setIcon(icone[0]);
repaint();
}
public void Think()
{
try {Thread.sleep((int)Math.random()*10000+3000);}
catch (InterruptedException e) {e.printStackTrace();}
}
public void anEat()
{
this.status = "EATING";
this.setToolTipText(name+" "+status);
this.setIcon(icone[2]);
repaint();
}
public void Eat()
{
try {Thread.sleep((int)Math.random()*10000+3000);}
catch (InterruptedException e) {e.printStackTrace();}
table.releaseForks(id);
}
public void anHungry()
{
this.status = "HUNGRY";
this.setToolTipText(name+" "+status);
this.setIcon(icone[1]);
}
public void Kill (){ this.c = false; }
}