package net.catpad.infobus.test.dinner;
import net.catpad.infobus.ServiceId;
public class Philosopher {
int number;
ServiceId id;
int eatNumber = 0;
public Philosopher(int number, ServiceId id) {
this.number = number;
this.id = id;
}
public ServiceId getServiceId() {
return id;
}
public int getLeftFork() {
return (number+1) % InfoBusTestDinner.PHILOSOPHERS_NUMBER;
}
public int getRightFork() {
return number;
}
public Philosopher getLeftNeighbour() {
return InfoBusTestDinner.getPhilosopher( (number+1) % InfoBusTestDinner.PHILOSOPHERS_NUMBER );
}
public Philosopher getRightNeighbour() {
int n = ( (number == 0) ? (InfoBusTestDinner.PHILOSOPHERS_NUMBER-1) : (number-1) );
return InfoBusTestDinner.getPhilosopher(n);
}
public void eat() throws InterruptedException {
eatNumber++; // keep statistics
System.out.println(toString() + " is eating...");
Thread.sleep(100);
System.out.println(toString() + " has finished his meal.");
}
public void think() throws InterruptedException {
System.out.println(toString() + " is thinking...");
Thread.sleep(100);
System.out.println(toString() + " has finished thinking and is going to be hungry...");
}
public String toString() {
return "Philosopher " + number;
}
public void printStatistics() {
System.out.println(toString() + " ate " + eatNumber + " times");
}
}
|