Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this homework, you will implement a multithreaded solution to finding the sum

ID: 3716548 • Letter: I

Question

In this homework, you will implement a multithreaded solution to finding the sum of 9,000,000 double values. Begin, by creating a method that creates an array (not an arrayList) of 9000000 double’s and populates each index with a random number. Create a class to hold the sum of all the numbers in the array. Protect access to that sum by using a ReentrantLock appropriately. Make sure you use good object oriented techniques in creating this class. Then, create a Runnable that can sum an array of any length and add that sum to a shared total. Separate the array into a minimum of 10 pieces and launch a thread to sum each of the pieces. You probably want more threads, but experiment to find number of threads that seems to sum the quickest. When all threads are done summing their pieces, show the combined sum of all 9,000,000 pieces

Explanation / Answer

/******************************************************************************

Online Java Compiler.

Code, Compile, Run and Debug java program online.

Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/

import java.util.Random;

import java.math.BigDecimal;

import java.util.concurrent.locks.ReentrantLock;

//Class that have the sum of array value and protected with the lock

//BigDecimal is used because the Addition of all decimal number will have the large value

class ArraySum

{

ReentrantLock _rLock = new ReentrantLock();

BigDecimal _arraySum = new BigDecimal("0");   

public void UpdateSum(BigDecimal value)

{

_rLock.lock();

  

_arraySum = _arraySum.add(value);

  

_rLock.unlock();

}   

  

public BigDecimal getArraySum()

{

return _arraySum;

}

}

//Class implement Runnable and Add the sum of given Array

class CountThread implements Runnable{  

Double[] _array;

ArraySum _arrayInstanse;

BigDecimal _arraySum = new BigDecimal("0");  

CountThread(Double[] array, ArraySum arrayInstanse)

{

_array = array;

_arrayInstanse = arrayInstanse;

}

public void run(){  

  

for(int i = 0 ; i < _array.length ; i++)

{

_arraySum = _arraySum.add(new BigDecimal(_array[i]));

}

  

_arrayInstanse.UpdateSum(_arraySum);

}

}

//Main class which have the main function and create the array with Random numbers

public class Main

{

public static void main(String[] args) {

  

Double[] array = new Double[9000000];

Random rand = new Random();

  

for(int i = 0 ; i < 9000000; i++)

{

array[i] = rand.nextDouble()*10000000000000000L;   

}

  

ArraySum arraySum = new ArraySum();

int indexOfLargeArray = 0;

for(int i = 0; i < 10; i++)

{

//Deviding the big array in small arrays

Double[] newArray = new Double[900000];

for(int j = 0 ; j < 900000; j++,indexOfLargeArray++)

{

  

newArray[j] = array[i];  

}

  

//Creating instane of CountThread class and starting the thread

Runnable countThread = new CountThread(newArray,arraySum);

new Thread(countThread).start();

}

  

try

{

// Sleep the main thraed for 3 seconds so that all the thread will finish their task.

Thread.sleep(3000);

System.out.println("ARRAY SUM = "+arraySum.getArraySum());

}

catch(Exception e)

{

  

}

  

  

}

}

//PLEASE PROVIDE FEEDBACK THUMBS UP

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote