/* * Rick van der Zwet * 0433373 * Java programming assigment 1.2 * Licence: BSD * $Id: Database.java 270 2007-10-13 13:45:29Z rick $ */ class Database { // Read lock, call next person // Write lock, call next person when done private int readlock = 0; private boolean writelock = false; private boolean writerequest = false; private String value = new String("INIT"); Database () {}; // constructor synchronized void get_readlock () { while (writelock || writerequest) { try { wait(); } catch (InterruptedException e) { System.out.println("interruptedException caught"); } } readlock++; } synchronized String read () { return (value); } synchronized void release_readlock () { readlock--; notify(); } synchronized void get_writelock() { writerequest = true; while (writelock && readlock > 0) { try { wait(); } catch (InterruptedException e) { System.out.println("interruptedException caught"); } } writelock = true; writerequest = false; } synchronized void write (String text) { value = value + ":" + text; } synchronized void release_writelock() { writelock = false; notify(); } }