Philosopher implementation

Philosopher implementation

class Philosopher extends Thread {

int identity; Chopstick left; Chopstick right;

Philosopher(Chopstick left, Chopstick right) {

this.left = left; this.right = right;

}

public void run() {

while (true) {

try {

sleep(…); // thinking

right.get(); left.get(); // hungry

sleep(…) ; // eating

right.put(); left.put();

} catch (InterruptedException e) {}

}

}

}

The Philosopher class is a straight forward translation from the specification for PHIL.

Each philosopher is a process with its own thread of control.

Previous slide Next slide Back to the first slide View Graphic Version