Question: Run the application and take measurements (max time to completion for all threads when the processing is divided between 1,2 and 4 threads. Take the
Run the application and take measurements (max time to completion for all threads when the processing is divided between 1,2 and 4 threads. Take the max time to completion for each of those three configurations and populate the table below. Each of the modifications requires a change of the source code. Make sure the system does not run in power saving mode which would adversely affect the measurements. Proposed table layout: Copy the data into an Excel spreadsheet and plot a line graph. Copy the table and the line graph into your answer sheet.
/* * Multi-Threading application * */ package multithreading;
import java.util.Random;
public class MultiThreading extends Thread {
private int id; // thread number
private static double[] shareddata; public MultiThreading(int i) { id = i; }
/* run method of the thread */ public void run() { int a;
Random generator = new Random(); long t = System.currentTimeMillis()/1000; System.out.println(" Thread " + id + " is starting"); if (id == 1) { for (a=0; a < 25000000; a++) { shareddata[a]=Math.cos(a+Math.sqrt(a*generator.nextDouble())); } } if (id == 2) { for (a=25000000; a <50000000;a++) { shareddata[a]=Math.cos(a+Math.sqrt(a*generator.nextDouble())); } } if (id == 3) { for (a=50000000; a <75000000;a++) { shareddata[a]=Math.cos(a+Math.sqrt(a*generator.nextDouble())); } } if (id == 4) { for (a=75000000; a <100000000;a++) { shareddata[a]=Math.cos(a+Math.sqrt(a*generator.nextDouble())); } } System.out.println("Thread " + id + " took " + (System.currentTimeMillis()/1000 - t) + " seconds");
}
public static void main(String[] args) { // number of threads final int N = 4; shareddata = new double[100000000]; // System.out.println("Starting Multi-threading...");
// creating thread array MultiThreading[] thread = new MultiThreading[N];
for (int i = 0; i < N; i++) { /* initialise each thread */ thread[i] = new MultiThreading(i+1); /* start each thread */ thread[i].start(); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
