/** * A table with forks. * * @author Franck van Breugel */ public class Table { private boolean[] available; /** * Number of seat on a table. */ public static int SIZE = 5; /** * Initializes this empty table. */ public Table() { this.available = new boolean[Table.SIZE]; for (int id = 0; id < Table.SIZE; id++) { this.available[id] = true; } } /** * 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.available[id]) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.available[id] = false; } /** * Put down the fork on the table with the given id. * * @param id id of the fork. */ public synchronized void putDown(int id) { this.available[id] = true; this.notifyAll(); } }