Question

IN JAVA PLEASE!!! :) Multithreading can help in achieving parallelism in computational problems. This makes the...

IN JAVA PLEASE!!! :)

Multithreading can help in achieving parallelism in computational problems. This makes the program’s response to generate output faster. It is achieved by delegating independent tasks within the program to separate threads instead of creating a sequential routine. Consider the following sample double array: 3 11 5 19 1 8 4 16 7 18 17 6 3 23 9 If the problem is to display all the row-sums and all the column-sums, a sequential program would use loops. In that case, the second row (or, column) sum cannot be computed before completing the first row (or, column) sum. However, we can observe that calculating and displaying a particular row (or, column) sum does not depend on calculation and display of other row (or, column) sums. Hence, the inherent parallelism in the problem calls for multithreading. In this assignment you are required to execute the above task in multithreaded way. In particular, the program should read a double array (as above) from a file (whose name would be supplied by the user) and display all row sums and all column sums. Format of the output should be: Thread i calculates the row-i sum as … Thread j calculates the column-j sum as … If the double array dimension is m x n, then the program will have m x n threads to take care of m row sums and n column-sums. Note, your program may need to decide how many separate threads are needed, after reading the data from the supplied file. Submission: You can use Java (preferred) to solve the problem. Include your name, course name, semester, and assignment identifier (Assign 1) as program comments at the top of your code. Submit the source file(s) through BlazeVIEW submission box.

rowandcolum file:

88   79   86   115   87   26   20   114   109   0   61   75   48   59   99  
36   22   53 30   17 106   0 90 12   32   7   58   31   94   17  
78   83   112   74   34   102   0   45 34   66   75   117   61 115  96  
28   11   76 41 87 63 69  109 73   48   99   1   69   34   67  
13   93   10   39 117 36  104   73 73 11  104   29   49   89   111  
110   4   83   78   63   88   22   99   19   115   59   65   91   16   96  
81   104   54   12   80   46   101   3   19   59   0   8   109   28   64  
47   49   33   42   87   69   111   55   82   84   68   50   15   7   16

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

class ThreadArraySum extends Thread{

public int[][] arrayToSum;
public int threadID;
public int totalSum;

public ThreadArraySum(int[][] arr, int threadID){
this.arrayToSum = arr;
this.threadID = threadID;
this.setTotalSum(0);
}

public void run() {
int arrayCol = arrayToSum[0].length;
int arrayRow = arrayToSum.length;
int colStart = (int)((threadID%2) * (arrayCol/2));
int rowStart = (int)((int)(threadID/2) * (arrayRow/2));
int colEnd = colStart + (int)(arrayCol/2);
int rowEnd = rowStart + (int)(arrayRow/2);

for(int i = colStart; i < colEnd; i++){
for(int j = rowStart; j < rowEnd; j++){
setTotalSum(getTotalSum() + arrayToSum[j][i]);
}
}
}

public int getTotalSum() {
return totalSum;
}

public void setTotalSum(int totalSum) {
this.totalSum = totalSum;
}

}
public class StartPoint {

public final static int cols = 15;
public final static int rows = 10;
public static int[][] arrayToAdd = new int[rows][cols];
static Random rand = new Random();
public static ThreadArraySum a0, a1, a2, a3;

public static void main(String[] args) throws InterruptedException{

for(int j = 0; j < rows; j++){
for(int i = 0; i < cols; i++){
arrayToAdd[j][i] = rand.nextInt(25);
}
}

  
  

a0 = new ThreadArraySum(arrayToAdd, 0);
a1 = new ThreadArraySum(arrayToAdd, 1);
a2 = new ThreadArraySum(arrayToAdd, 2);
a3 = new ThreadArraySum(arrayToAdd, 3);
  

long startTimeMultiThreaded = System.nanoTime();
a0.start();
a1.start();
a2.start();
a3.start();

a0.join();
a1.join();
a2.join();
a3.join();
int Sum = addThreadSum();
long estimatedTimeMultiThreaded = System.nanoTime() - startTimeMultiThreaded;

System.out.println("The total sum calculated by multi threaded program is: " + Sum);
System.out.println("The total time taken by multi threaded program is: " + estimatedTimeMultiThreaded);
}

public static int addThreadSum(){
return a0.getTotalSum() + a1.getTotalSum() + a2.getTotalSum() + a3.getTotalSum();
}
}

Add a comment
Know the answer?
Add Answer to:
IN JAVA PLEASE!!! :) Multithreading can help in achieving parallelism in computational problems. This makes the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • NEED C++ HELP. PLEASE FOLLOW DIRECTIONS CAREFULLY: Use a 1 dimensional array object to create a...

    NEED C++ HELP. PLEASE FOLLOW DIRECTIONS CAREFULLY: Use a 1 dimensional array object to create a 2 dimensional table object. Then modify to create a triangular table. Example: Objective -> create an array of dynamic objects of RowAray inside Table. See below for RowAray.h, Table.h, Then create a triangular table like the example above. Fill each cell with random 2 digit integers. The Example Table above has 8 columns of RowAray objects each filled with 6 rows of random 2...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • Consider the below matrixA, which you can copy and paste directly into Matlab.

    Problem #1: Consider the below matrix A, which you can copy and paste directly into Matlab. The matrix contains 3 columns. The first column consists of Test #1 marks, the second column is Test # 2 marks, and the third column is final exam marks for a large linear algebra course. Each row represents a particular student.A = [36 45 75 81 59 73 77 73 73 65 72 78 65 55 83 73 57 78 84 31 60 83...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • Having some coding issues. I can't seem to figure out why my calculation is coming out...

    Having some coding issues. I can't seem to figure out why my calculation is coming out incorrect for the fourth column. For example, mathematically speaking the first row, columns 1-3: ​19, 68, 10. Using the specific equation: finalGrade = e1*.30 + e2*.30 + f*.40. I should get 30.1 in the 4th column but I get otherwise. See provided screenshot. Please explain what I am doing wrong? /* Program info: Fill dd array 30 and 3 of grades Double course[30][3](rows =...

  • The data from data421.dat contains information on 78 seventh-grade students. We want to know how well...

    The data from data421.dat contains information on 78 seventh-grade students. We want to know how well each of IQ score and self-concept score predicts GPA using least-squares regression. We also want to know which of these explanatory variables predicts GPA better. Give numerical measures that answer these questions. (Round your answers to three decimal places.) find:(Regressor: IQ) R 2 find: (Regressor: Self-Concept) R 2 obs gpa iq gender concept 1 7.94 103 2 54 2 8.292 111 2 73 3...

  • How to create this program and makes its run with the giving the class PA4Driver with...

    How to create this program and makes its run with the giving the class PA4Driver with the main method and the unsortedItems.txt. Write a program that will read a file (unsortedItems.txt)with a number of items. Your program will then store each item that was read form the file to an Arrayof items. Now that you have all items stored in the Array you need to sort them based on the uid (unique id) and write them to another file (sortedItems.txt)....

  • I literally have no idea what I’m doing. Help!!! Please show work!! Write an essay using...

    I literally have no idea what I’m doing. Help!!! Please show work!! Write an essay using Word and post it as an attachment to the discussion. Cover the following points. Choose a data set from Stat Disk using one of the following files from 13th Edition Elementary Statistics: Oscar Winner Freshman 15 Word Count Garbage Weights Passive and Active Smoke If the file contains more than one variable, choose a quantitative variable. Create a histogram using your chosen variable and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT