FileDocCategorySizeDatePackage
Philosopher.javaAPI DocExample3014Tue Dec 12 18:57:48 GMT 2000None

Philosopher

public class Philosopher extends Thread

Fields Summary
private Chopstick
leftStick
private Chopstick
rightStick
private double
x
private double
y
private boolean
sated
private PhilosopherArea
parent
private int
position
private boolean
stopRequested
Constructors Summary
public Philosopher(PhilosopherArea parent, double x, double y, int position)


      
		             
        super(parent.names[position]);

        this.parent = parent;
        this.position = position;
        this.x = x;
        this.y = y;

        // identify the chopsticks to my right and left
        this.rightStick = this.parent.chopsticks[position];
        if (position == 0) {
            this.leftStick = this.parent.chopsticks[this.parent.NUMPHILS-1];
        } else {
            this.leftStick = this.parent.chopsticks[position-1];
        }

        // I'm hungry
        this.sated = false;
    
Methods Summary
public voideat()

        rightStick.release(this);
        leftStick.release(this);
        sated = true;
        parent.repaintPhil(position);
    
public voidpaint(java.awt.Graphics g)

        g.setColor(Color.lightGray);
        g.fillRect((int)x, (int)y, parent.imgs[0].getWidth(parent),
		   parent.imgs[0].getHeight(parent)+25);
        if (sated == false) {
            if (rightStick.owner == this && leftStick.owner != this) {
		// got left only
                g.drawImage(parent.imgs[1], (int)x, (int)y, parent);
            } else if (rightStick.owner == this && leftStick.owner == this) {
		// got both
                g.drawImage(parent.imgs[2], (int)x, (int)y, parent);
            } else {
		// got nothing
                g.drawImage(parent.imgs[0], (int)x, (int)y, parent);
            }
        } else {
            g.drawImage(parent.imgs[0], (int)x, (int)y, parent);
            g.setColor(Color.black);
            g.drawString("Mmm!", ((int)(x)+8),
			 ((int)(y)+parent.imgs[0].getHeight(parent)+13));
        }
    
public voidrun()

        int grabDelay;
        while (stopRequested == false) {
             try {
                grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
                sleep((int)(Math.random() * grabDelay));
                rightStick.grab(this);
                parent.repaintPhil(position);

                grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
                sleep((int)(Math.random() * grabDelay));
                leftStick.grab(this);
                parent.repaintPhil(position);

                grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
                sleep((int)(Math.random() * grabDelay));
                eat();

                grabDelay = parent.controller.grabDelaySlider.getValue() * 100;
                sleep((int)(Math.random() * grabDelay * 4));
                sated = false;
                parent.repaintPhil(position);
            } catch (java.lang.InterruptedException e) {
            }
        }
    
public voidstopRequested()

        stopRequested = true;