Question

In: Computer Science

Write an application that uses multithreading to compute the sum of the integers in an array...

Write an application that uses multithreading to compute the sum of the integers in an array of size 100,000. You can populate the array with random numbers and your application should display the sum. (JAVA)

Solutions

Expert Solution

Program Screenshots:

Sample Output:

Code to be Copied:

DriverProgram.java

//import required package
import java.util.Random;

// Application driver code to compute sum
public class DriverProgram
{
   //declare local variable
   public static final int SIZE = 100000;

   //main method
   public static void main(String[] args)
   {
       //create object for random
       Random r = new Random();
       //create array of size
       int[] array = new int[SIZE];
       //use for loop to create random numbers
       for (int i = 0; i < array.length; i++)
       {
           array[i] = r.nextInt(100001) + 1;
       }      
       //call the method in the mutithread class
       long arraySum= MultiThreading.multiThreadSum(array);
       //print the result
       System.out.println("Array sum using MultiThreading is: " + arraySum);
   }
}


MultiThreading.java

//Create class Application which extends thread
public class MultiThreading extends Thread
{
   //Declare 8 threads
   public static final int NUM_THREADS = 10;
   private int[] array;
   private int start, end, sum;

   //create constructor for the class
   public MultiThreading(int[] inputarr, int l, int h)
   {
       this.array = inputarr;
       this.start = l;
       this.end = Math.min(h, inputarr.length);
   }

   //implement run method
   //to invoke when thread starts
   public void run()
   {
       //call the other method to compute the sum
       //for each thred
       sum = eachThreadSum(array, start, end);
   }

   //Implement method sum to compute eachThreadSum
   public static int eachThreadSum(int[] arr, int low, int high)
   {
       //declare variable
       int total = 0;
       //use for-loop to compute the total
       for (int i = low; i < high; i++)
       {
           total += arr[i];
       }
       //return total
       return total;
   }
   //implement method to compute sum for multithreads
   public static int multiThreadSum(int[] arr)
   {
       //call sub method for the array and numthreads
       return multiThreadSum(arr, NUM_THREADS);
   }

   //implement method to compute sum
   public static int multiThreadSum (int[] arr, int threads)
   {
       //get the size of the array of each thread
       int size = (int) Math.ceil(arr.length * 1.0 / threads);
      
       MultiThreading[] threadSum = new MultiThreading[threads];

       //use for-loop to compute sum
       for (int i = 0; i < threads; i++)
       {
           threadSum[i] = new MultiThreading(arr, i * size, (i + 1) * size);
           //invoke start method to implement run method
           threadSum[i].start();
       }

       //join each thread
       try
       {
           for (MultiThreading sum : threadSum)
           {
               sum.join();
           }
       }
       //if the sum is not calculated
       //throw exception contains message
       catch (InterruptedException e)
       {
           e.getMessage();
       }
       //compute sum by calling method
       int total = 0;
       for (MultiThreading sum : threadSum)
       {
           total += sum.getPartialSum();
       }
       //return total
       return total;
   }
   //accessor method
   public int getPartialSum()
   {
       return sum;
   }
}


Related Solutions

Write a program to compute intersection of two sorted array of integers and compute the CPU...
Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator. Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE NO HASH SET
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write a function sum_int( n ) to compute the sum of the integers from 1 up...
Write a function sum_int( n ) to compute the sum of the integers from 1 up to and including n.
Consider the problem of finding if an array contains a pair of integers with a sum...
Consider the problem of finding if an array contains a pair of integers with a sum of 100. The array contains n integers. a. Define the signature (header) of a C++ function that solves this problem (hint: inputs/outputs of function) b. Write the pseudocode or C++ body of the function (extra credit: write the most efficient algorithm) c. What is the asymptotic complexity of your algorithm? d. What would be the complexity of the best algorithm for this problem if...
Create an application containing an array that stores 5 integers. The application should call five methods...
Create an application containing an array that stores 5 integers. The application should call five methods from Array2 class that in turn (1) display all the integers, (2) display all the integers in reverse order, (3) display the sum of the integers, (4) display all values less than a limiting argument, and (5) display all values that are higher than the calculated average value. Save the file as ArrayTest.java.
1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
write code to count the number of odd integers in an array of 100 random integers...
write code to count the number of odd integers in an array of 100 random integers in the range [0,99].
// This program uses a bubble sort to arrange an array of integers in // ascending...
// This program uses a bubble sort to arrange an array of integers in // ascending order (smallest to largest). It then display the array // before the sorting and after the sorting. Modify the program so it orders // integers in descending order (largest to smallest). Then add some code // to display the array at each step of the algorithm. You don't have to // modify anything in the main() function. All modification are inside // the bubbleSortArray()...
Write array methods that carry out the following tasks for an array of integers by creating...
Write array methods that carry out the following tasks for an array of integers by creating and completing the “ArrayMethods” class below. Add documentation comments for each method. Provide a test program called ‘Lab5_yourID.java” that test methods of ArrayMethods class. In your test program, use random class to generate array values. public class ArrayMethods { private int[ ] values; //declare instant variables public ArrayMethods (int[ ] initialValues) {values = initialValues;} //constructor public void shiftRight( ) { … } public Boolean...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT