/* * Thread run function */ public void Run() { Console.WriteLine("Starting producer..."); // print that producer has started // loop over each production cycle for (int iProd = 1; iProd <= m_numProductions; iProd++) { // diplay information about current priority queue Console.WriteLine("Current priority queue has " + m_heap.Size() + " nodes."); Console.WriteLine("Producer adding " + m_numProcesses + " nodes."); int idleTime = randomIdle.GetNext(); // produce the process for the heap for (int iProc = 1; iProc <= m_numProcesses; iProc++) { int id = iProd + iProc + (iProd - 1) * m_numProcesses - iProd; //create a unique process id int priority = randomPriority.GetNext(); //get random priority int timems = randomTimeSlice.GetNext(); //get random time slice m_heap.InsertNode(new HeapNode(id, priority, timems)); //add process to heap } //sleep the producer Console.WriteLine("Producer going to sleep."); Thread.Sleep(idleTime); } m_productionFinished.Set(true); // flag that the producer has finished Console.WriteLine("Producer is exiting."); }
/* * thread run method that handles the consumption */ public void Run() { Console.WriteLine(m_prefix + "Consumer " + m_name + " is starting."); // consumer has spun up while (!m_productionFinished.Get() || m_heap.Size() != 0) { // check for empty heap and finished production if (m_heap.Size() == 0) { // if heap is empty, then idle to allow producer to finish producing Console.WriteLine(m_prefix + "Consumer " + m_name + " is idle."); Thread.Sleep(m_idle); } else { // consume the next process String message = Consume(); Console.WriteLine(m_prefix + "Consumer " + m_name + " finished " + message); } } Console.WriteLine(m_prefix + "Consumption by consumer " + m_name + " has finished."); Console.WriteLine(m_prefix + "Consumer " + m_name + " completed " + m_count + " processes."); }