Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Fill in the rest of this Java Code Concurrency is becoming a very important programming concept due to the explosion of multi-threaded, multi-core processors. You

Fill in the rest of this Java Code

Concurrency is becoming a very important programming concept due to the explosion of multi-threaded, multi-core processors. You are to write a simple concurrent program in JAVA. You will create a square two dimensional matrix and calculate some basic statistics while measuring the time. The specifications are as follows:

here is some starter code for you fill in:

--------------------------------------------------------------------- // This is a very small set up to get people started on using threads // We need static variable pointers in the main class so that // we can share these values with the threads. // the threads are address separate from us, so we need to share // pointers to the objects that we are sharing and updating import java.util.ArrayList; public class MythreadTest { private static ArrayList arrThreads = new ArrayList(); // we use static variables to help us connect the threads // to a common block public static int N=0; public static int[][] A; //main entry point for the process public static void main(String[] args) { try { int size = Integer.parseInt(args[0]); // create the array from input A = new int[size][size]; // // fill array with random valuesvalues // // create N threads to work on each row for (int i = 0; i < 10; i++) { Thread T1 = new Thread(new ThreadTest(i)); T1.start(); // standard thread start arrThreads.add(T1); } // wait for each thread to complete for (int i = 0; i < arrThreads.size(); i++) { arrThreads.get(i).join(); } // all the threads are done // do final calculations System.out.println("Main Thread has N as value " + N) ; //This for loop will not stop execution of any thread, //only it will come out when all thread are executed

System.out.println("Main thread exiting "); } catch (Exception e) { System.out.println(e.getMessage()); } } } // each thread should access its row based on "ind" // and leave results I would suggest in a static array that you need // to create in MythreadTest class ThreadTest implements Runnable { private int i; ThreadTest(int ind) { i = ind; } public void run() { try { MythreadTest.N += i ; // this is a global variable in MythreadTest we add stuff together; System.out.println("Thread is started " + i + "Array is " + MythreadTest.A[1][3]); Thread.sleep(1000); System.out.println("Thread is exiting " + i); } catch (Exception e) { System.out.println(e.getMessage()); } } }

---------------------------------------

The program takes1 input from the user (or on command line) 1) the dimension of the matrix- N

Your program will do the following 1) Create an NxN two dimension INTEGER matrix 2) randomly assign INTEGER values to each element in the range of between 2^(32-N) and 2^(31-N). The best way to do this is to do the following (assuming random() is in the range [0,1). ) trunc( (2^(32-N) - (2^31-N))* Random() + 2^(31-N))

This is basically : range * Random() + start_value

START TIMER NOW 3) You will create N threads, each thread is responsible for one row of the matrix 4) each thread will calculate the max, min and what it needs to report for summation/average 5) You should add a common set of arrays in the main object class to allow each thread to copy values back to the main thread. 6) Your main thread will wait on all of the children threads, and then calculate the overall max, Min and average

STOP TIMER

You will need to calculate and report the following from the matrix a) The maximum value b) The minimum value c) The average of all of the values in the matrix d) the time it took to do parts a-c

You may ONLY use INT and FLOAT for your calculation of MAX, SUM and average You MAY ONLY use DOUBLE/LONG for time values. If you use larger primitives to solve sum and average you will see 50% off for any use of these primitive data types.

Ensure your results look valid. In particular the average.

Your runtime needs to report average time run, and standard deviation.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Accounting questions

Question

What is the formula to calculate the mth Fibonacci number?

Answered: 1 week ago