public class ActiveObject extends Thread { private int data; private static int numberofobjects=0; private int mynumber; private SharedQueue inmessages, outmessages; public ActiveObject() { data=0; inmessages=new SharedQueue(); outmessages=new SharedQueue(); numberofobjects+=1; mynumber=numberofobjects; } public void sendmessage(Object o) { inmessages.add(o); } public boolean responseready() { return (!outmessages.empty()); } public String getresponse() { return ((String)outmessages.remove()); } public void run() { System.out.println("[ActiveObject#"+mynumber+" started]"); while (true) { if (inmessages.empty()) { data+=1; try { Thread.sleep(1000); } catch (InterruptedException e) { } } else { String message=(String)inmessages.remove(); if (message.equals("")) data+=1; else if (message.equals("stop")) break; else if (message.equals("reset")) data=0; else if (message.equals("report")) outmessages.add("ActiveObject#"+mynumber+": value="+data); else if (message.equals("twice")) data*=2; } } System.out.println("[ActiveObject#"+mynumber+" terminated]"); } }