// COM3336 Operating Systems // Homework 1 part 2 // John Sung // Started: 10/9/1999 // // // 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. private int ThreadCount; // constructor for this class ThreadMutex(int initialValue) { // set the initial value for this mutex. // usually 0. ThreadCount = initialValue; } // change the thread count number, threadCount can be a negative number. public synchronized void changeThreadCount(int threadCount) { // add the threadCount to the running thread count. ThreadCount += threadCount; // if the threadcount is less than zero then print some kind of // error and just reset the thread count. if (ThreadCount < 0) { System.out.println("Warning, Thread Count less than zero"); System.out.print("Thread count = "); System.out.println(ThreadCount); ThreadCount = 0; } } // checking to see if the threads have finished public boolean areThreadsFinished() { // if the thread count is zero then return true, // else return false. if (ThreadCount == 0) return true; else return false; } }