package philo;
public class Table {
    boolean forks[];
    int eatctr;

public    Table() {
        forks = new boolean[Philo.NUM_PHIL];
        for (int i = 0; i < Philo.NUM_PHIL; ++i)
            forks[i] = true;
    }

    int getForks(int id) throws InterruptedException {
        synchronized (this){
        int id1 = id;
        int id2 = (id + 1) % Philo.NUM_PHIL;
//        System.out.println(id + " check forks[" + id1 + "]=" + forks[id1] + " and forks[" + id2 + "]="+forks[id2]);
//        System.out.flush();
        while(! (forks[id1] && forks[id2])) {
//            System.out.println(id + " wait for forks");
            wait();
        }
        forks[id1] = forks[id2] = false;
//        System.out.println(id + " got forks");
        return eatctr++;
        }
    }

    synchronized void putForks(int id) {
//        System.out.println(id + " putforks");
        forks[id] = forks[(id + 1) % Philo.NUM_PHIL] = true;
        notify();
//        System.out.println(id + " notify done");
    }
}