/** * A table with forks. * * @author Franck van Breugel */ public class Table { private int size; private boolean[] pickedUp; /** * Initializes this empty table. * * @param size the size of this table. */ public Table(int size) { this.size = size; this.pickedUp = new boolean[size]; // all false } /** * Pick up the fork from this table with the given id. * * @param id id of the fork. */ public synchronized void pickUp(int id) { while (this.pickedUp[id % this.size]) { try { this.wait(); } catch (InterruptedException e) {} } this.pickedUp[id % this.size] = true; } /** * Put down the fork on the table with the given id. * * @param id id of the fork. */ public synchronized void putDown(int id) { this.pickedUp[id % this.size] = false; this.notifyAll(); } }