import java.io.*; // the mutex class to keep track of the number of spawned threads // so that the thread that spawned the threads can wait for all of // the spawned threads are done. public class ThreadMutex { /////////////////////////////////////////// // private variables // the counter to count the number of threads to wait for // to end private int ThreadCount; // the print stream to output from the services PrintStream Outstream; // constructor for this class ThreadMutex(int numOfThreads, PrintStream out) { ThreadCount = numOfThreads; Outstream = out; } // output to the outstream stored in here public synchronized void outputToStream(ByteArrayOutputStream byteStream) { try { byteStream.writeTo(Outstream); } catch (IOException ex) { } } // just wait until the thread count is == 0 public synchronized void waitForThreads() { try { while(ThreadCount > 0) wait(); } catch (InterruptedException ex) { } } // decrement the counter public synchronized void decrementThreadCount() { ThreadCount--; notifyAll(); } }