The following is a piece of multi-threading code. Thread 1 will generate the ran
ID: 3747709 • Letter: T
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 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.
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();
}
}
Explanation / Answer
public class Sum implements Runnable {
int a;
public Sum(int a) {
this.a = a;
}
public void run() {
addRandom();
}
public void addRandom() {
Random rand = new Random();
int n = rand.nextInt(10) + 1;
System.out.println("numbers generated: " + n);
synchronized (this) {
a += n;
}
}
}
public static void main(String[] args) {
int base = 0;
Sum sum2 = new Sum(base);
Thread t1 = new Thread(sum2);
Thread t2 = new Thread(sum2);
Thread t3 = new Thread(sum2);
Thread t4 = new Thread(sum2);
Thread t5 = new Thread(sum2);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
try {
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("final results: " + sum2.a);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.