Question
The following is a piece of multi-threading code. Thread 1 will generate the random number, Thread 2 will sort the random number generated by Thread
The following is a piece of multi-threading code. Thread 1 will generate the random number, Thread 2 will sort the random number generated by Thread 1 .
Answer the following questions;
Run this sample code and show the result by using the screenshots. Refer this sample code, write a multi-threading code that contains 3 threads: Thread1 will generate the random number; Tread 2 will generate the sum of the random numbers generated so far; Thread 3 will find the max among the random numbers generated so far. // sample code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Collections; class mutex { // Create a new Mutex. The creating thread does not own the mutex. private static Mutex mut = new Mutex(); private const int numIterations = 1; private const int numThreads = 3; public static int myNum = 0; public static ArrayList myAL = new ArrayList(); static void Main() { // Create the threads that will use the protected resource. Thread t1 = new Thread(new ThreadStart(ThreadProc1)); t1.Name = String.Format("Thread 1"); t1.Start(); Thread t2 = new Thread(new ThreadStart(ThreadProc2)); t2.Name = String.Format("Thread 2"); t2.Start(); // The main thread exits, but the application continues to // run until all foreground threads have exited. } public static void ThreadProc1() { Random random = new Random(); for (int i = 0; i < 10; i++) { UseResource1(); } } private static void ThreadProc2() { for (int i = 0; i < 10; i++) { UseResource2(); } } // This method represents a resource that must be synchronized // so that only one thread at a time can enter. public static void UseResource1() { Random random = new Random(); // Wait until it is safe to enter. Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name); mut.WaitOne(); Console.WriteLine("{0} has entered the protected area", Thread.CurrentThread.Name); if (myNum == 0) { myNum = random.Next(1, 100); Console.WriteLine("Number Generated : {0}", myNum ); } // Place code to access non-reentrant resources here. Thread.Sleep(500); Console.WriteLine("{0} is leaving the protected area", Thread.CurrentThread.Name); // Release the Mutex. mut.ReleaseMutex(); Console.WriteLine("{0} has released the mutex", Thread.CurrentThread.Name); } private static void UseResource2() { // Wait until it is safe to enter. Console.WriteLine("{0} is requesting the mutex", Thread.CurrentThread.Name); mut.WaitOne(); Console.WriteLine("{0} has entered the protected area", Thread.CurrentThread.Name); // Place code to access non-reentrant resources here. if (myNum != 0) { myAL.Add( myNum ); myNum = 0; // Sorts the values of the ArrayList. myAL.Sort(); // Displays the values of the ArrayList. Console.WriteLine("After sorting:"); PrintValues(myAL); } // Simulate some work. Thread.Sleep(500); Console.WriteLine("{0} is leaving the protected area", Thread.CurrentThread.Name); // Release the Mutex. mut.ReleaseMutex(); Console.WriteLine("{0} has released the mutex", Thread.CurrentThread.Name); } public static void PrintValues(ArrayList myList) { for (int iIndex = 0; iIndex < myList.Count; iIndex++) { object o = myList[iIndex]; Console.Write(" {0}", o); } Console.WriteLine(); } } Run this sample code and show the result by using the screenshots.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started