/* Class Entry implements synchronization element that serves */ /* to implement rendez-vous */ public class Entry { // type of monitor /* * Every server`s entry (i.e. its method called from outside using * synchronous rendez-vous mechanismus needs to be logically connected with * an instance of the Entry class! Clien`t need not use the methods of Entry * class directly! */ private int state; // state of entry // 0 - nobody is waiting, 1 - server thread is waiting, // 2 - client threads are waiting, 3 - randez-vous in // progress (and other clients are possibly waiting) private int count; // num. of waiting client threads public Entry() { state = 0; count = 0; } public int get_state() { return state; } public int get_count() { return count; } /* * it is called by server-thread's run() at the entry point, in this point it * accepts all server calls internally synchronized using the same entry * instance */ synchronized public void accept_entry() { boolean flag = true; while (flag) { switch (state) { case 0: state = 1; try { wait(); } catch (InterruptedException e) { } break; case 1: flag = false; break; case 2: state = 3; count--; notify(); try { wait(); } catch (InterruptedException e) { } break; case 3: flag = false; if (count == 0) state = 0; else state = 2; } } } /* uses server-thread class at the body begin */ /* of an entry-procedure */ synchronized public void call_entry() { boolean flag = true; while (flag) { switch (state) { case 0: state = 2; count++; try { wait(); } catch (InterruptedException e) { } break; case 1: state = 3; flag = false; break; case 2: count++; try { wait(); } catch (InterruptedException e) {} case 3: flag = false; } } } /* uses server-thread class before at the body end */ /* of the entry-procedure */ synchronized public void release_entry() { notify(); } }