// producer consumer rmi version, server import java.util.ArrayList; import java.rmi.*; import java.rmi.server.*; class Dfactory extends UnicastRemoteObject implements DShared { int max; int waiting = 0; int busy = 0; public int capacity() throws RemoteException { return max; } public int waiting() throws RemoteException { return waiting; } public int busy() throws RemoteException { return busy; } public int empty() throws RemoteException {return max - waiting - busy; } ArrayList Slots; ArrayList Status; final int EMPTY = 0; final int WAITING = 1; final int BUSY = 2; final int INVALID = -1; int nextwaiting = INVALID; // next resource waiting to be consumed // even the constructor needs to declare exception public Dfactory(int m) throws RemoteException // constructor { max = m; Slots = new ArrayList(); Status = new ArrayList(); } public synchronized int produce(String food) throws RemoteException { int rid = 0; try { while (empty()<1) { System.out.println(this+": production of "+food+" blocked"); wait(); } rid = Status.size(); Slots.add(food); Status.add(WAITING); waiting++; if (waiting==1) nextwaiting = rid; // FIFO QUEUE notifyAll(); } catch (InterruptedException ie) { return INVALID; } return rid; }// produce public synchronized int consume() throws RemoteException { int rid = 0; try { while (waiting<1) wait(); waiting--; // take food! busy++; rid = nextwaiting; // System.out.println("nextwaiting:"+nextwaiting); Status.set(nextwaiting,BUSY); // reset nextwaiting if (waiting==0) nextwaiting = INVALID; else { while (nextwaiting=Slots.size() || Status.get(rid)!=BUSY) return; Status.set(rid,EMPTY); busy--; } // returns resource indicated by resource id, returns null if not valid. // this is always a non-blocking call. public synchronized String getresource(int rid) throws RemoteException { if (rid