Write a multithreaded C# program. Your program will have an array of integers as
ID: 3918481 • Letter: W
Question
Write a multithreaded C# program. Your program will have an array of integers as follows: int[] data = new int[10000000]; Create the following function static void calc( int startingIndex, int reps ) { // assign into the data array from startingIndex to // startingIndex + reps the following: // Math.Atan(i) * Math.Acos(i) * Math.Cos(i) * Math.Sin(i); } You will call calc twice. Each time you call it you need to get an elapsed time—how long did the function take (in milliseconds). The first time it will be not be in a separate thread as follows: calc( 0, data.Length ); The next time it is called it must be in four threads, splitting up the processing between all threads. Your program must output the following: Sequentially, calc takes 7654 milliseconds to run. Multithreaded, calc takes 3123 milliseconds to run.
Explanation / Answer
*****************************************************************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
namespace Rextester
{
public class Program
{
public static void Thread1(int a,int b)
{
calc(a,b);
}
public static void calc(int starting_index,int resp)
{
double result=0;
for(int i=starting_index;i<=(starting_index + resp);i++)
{
result=result +( Math.Atan(i)*Math.Acos(i)*Math.Cos(i)*Math.Sin(i));
}
}
public static void Main(string[] args)
{
//Your code goes here
int []data=new int[]{1,0,0,0,0,0,0,0};
double elapsed_time=0;
double start_time= DateTime.Now.Millisecond;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
//Console.Write(data.Length);
calc(0,data.Length);
stopwatch.Stop();
elapsed_time=DateTime.Now.Millisecond-start_time;
Console.WriteLine(elapsed_time);
Thread tid1 = new Thread(new ThreadStart(Thread1(0,1) ) );
Thread tid2 = new Thread(new ThreadStart(Thread1(2,3) ) );
Thread tid3 = new Thread(new ThreadStart(Thread1(4,5) ) );
Thread tid4 = new Thread(new ThreadStart(Thread1(6,7) ) );
tid1.Start();
tid2.Start();
tid3.Start();
tid4.Start();
}
}
}
**************************************************************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.