import java.util.*; // -------------------------------------------------- // By making this a class, you can easily add more information // for each of your Chat room guests class Chat { LinkedList m_lList = new LinkedList(); } // -------------------------------------------------- class ChatArea{ // You could make this more dynamic, but it's a little // simpler to keep the simple array approach Chat chatArr[]= new Chat[100]; boolean stopFlag = false; // Calling this routine should tell all of the chatserver // threads to terminate synchronized void setStopFlag() { stopFlag=true; notifyAll(); } // Add a new string to all linked lists synchronized void putString(int index, String s) { for (int i= 0; i < chatArr.length; i++) if (chatArr[i] != null) chatArr[i].m_lList.addLast(s); notifyAll(); System.out.println("putString: "+ s); } // called to get the list of strings awaiting any given // thread synchronized String getStrings(int index) { int i, num; String str; StringBuffer sb = new StringBuffer(""); LinkedList lList; lList = chatArr[index].m_lList; num=lList.size(); try{ for (i=0; i < num; i++) { str = (String)lList.removeFirst(); sb.append( str); sb.append("\n"); } } catch (NoSuchElementException e) { System.out.println("Our List Count is Messed Up???"); } return sb.toString(); } // called to wait for any new messages for a given thread synchronized String waitForString(int index) { String str; do { str = getStrings(index); try { if (str.length()== 0) wait(); } catch (InterruptedException e) { System.out.println("Interrupted wait call"); } if (stopFlag) return null; } while (str.length() == 0); return str; } // Cread a new chat data structure synchronized void addNewChat(int index) { chatArr[index] = new Chat(); } synchronized void removeChat(int index) { chatArr[index] = null; } }