Question

Create a program with threads that looks through a vary large array (100,000,000 elements) to find...

Create a program with threads that looks through a vary large array (100,000,000 elements) to find the smallest number in that array. You should track the current lowest value seen in a single, shared value. You should also track the time taken, comparing the amount of time for 1, 2, 4 and more threads. Fairly precise timing can be obtained by using the System.nanoTime() method. For example, it's taking my poor computer over 1 second to fill my array with random numbers:

    public static void main( String[] args){
        long beginTime = System.nanoTime();
        Random gen = new Random();
        int[] data = new int[100000000];
        for(int i = 0; i < data.length; i++) {
            data[i] = gen.nextInt();
            if( data[i] < 0) {
                int foo = 1;
            }
            else {
                int foo = 1;
            }
        }
        long endTime = System.nanoTime();
        System.out.println("Done filling the array. That took " + (endTime - beginTime) + " nanoseconds.");
    }

Please submit your java source code along with a short summary of your design and results.

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

Output (Result vary on different systems)

Code with comments

import java.util.Random;

public class Sample{

    static class MinMax implements Runnable{

        int []arr;

        int start,end,min,max;

        MinMax(int[]arr, int start,int end){

            this.start=start;

            this.end=end;

            min=Integer.MAX_VALUE;

            max=Integer.MIN_VALUE;

            this.arr=arr;

        }

        @Override

        public void run() {

            for(int i=start;i<=end;i++){    //search min and max form strant to end index

                min=Math.min(min,arr[i]);

                max=Math.max(max, arr[i]);

            }

        }

    }

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

        long beginTime = System.nanoTime();

        Random gen = new Random();

        int n=100000000;

        int[] data = new int[n];    //generate and fill random numbers

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

            data[i] = gen.nextInt()%1000000;

        }

        long endTime = System.nanoTime();

        System.out.println("Done filling the array. That took " + (endTime - beginTime)/1000000000f + " seconds.");

        //-----------------------------------------

        System.out.println("Using 1 thread");   //1 thread

        MinMax m1=new MinMax(data,0,n-1);   //class object

        Thread t1=new Thread(m1);   //new thread

        beginTime=System.nanoTime();    //start timer

        t1.start(); //start thread

        t1.join(0); //wait until thread finishes

        endTime=System.nanoTime();  //end timer

        System.out.println("Min,Max: "+m1.min+","+m1.max);  //print minimum and maximum

        System.out.println("Time using 1 thread " + (endTime - beginTime)/1000000000f + " seconds.");   //print time taken

        //-----------------------------------------

        System.out.println("Using 2 thread");

        m1=new MinMax(data,0,n/2);

        MinMax m2=new MinMax(data,n/2+1,n-1);

        t1=new Thread(m1);

        Thread t2=new Thread(m2);

        beginTime=System.nanoTime();

        t1.start();

        t2.start();

        t1.join(0);

        t2.join(0);

        endTime=System.nanoTime();

        System.out.println("Min,Max: "+ Math.min(m1.min,m2.min)+","+Math.max(m1.max,m2.max));

        System.out.println("Time using 2 thread " + (endTime - beginTime)/1000000000f + " seconds.");

        //-----------------------------------------

        System.out.println("Using 3 thread");

        m1=new MinMax(data,0,n/3);

        m2=new MinMax(data,n/3+1,2*n/3);

        MinMax m3=new MinMax(data,2*n/3+1,n-1);

        t1=new Thread(m1);

        t2=new Thread(m2);

        Thread t3=new Thread(m3);

        beginTime=System.nanoTime();

        t1.start();

        t2.start();

        t3.start();

        t1.join(0);

        t2.join(0);

        t3.join(0);

        endTime=System.nanoTime();

        System.out.println("Min,Max: "+ Math.min(Math.min(m1.min,m2.min),m3.min)+","+Math.max(Math.max(m1.max,m2.max),m3.max));

        System.out.println("Time using 3 thread " + (endTime - beginTime)/1000000000f + " seconds.");

        

        //-----------------------------------------

        System.out.println("Using 4 thread");

        m1=new MinMax(data,0,n/4);

        m2=new MinMax(data,n/4+1,2*n/4);

        m3=new MinMax(data,2*n/4+1,3*n/4);

        MinMax m4=new MinMax(data,3*n/4+1,n-1);

        t1=new Thread(m1);

        t2=new Thread(m2);

        t3=new Thread(m3);

        Thread t4=new Thread(m4);

        beginTime=System.nanoTime();

        t1.start();

        t2.start();

        t3.start();

        t4.start();

        t1.join(0);

        t2.join(0);

        t3.join(0);

        t4.join(0);

        endTime=System.nanoTime();

        System.out.println("Min,Max: "+ Math.min(Math.min(m1.min,m2.min),Math.min(m3.min,m4.min))+","+Math.max(Math.max(m1.max,m2.max),Math.max(m3.max,m4.max)));

        System.out.println("Time using 4 thread " + (endTime - beginTime)/1000000000f + " seconds.");

    }

}

For indentation purpose

Add a comment
Know the answer?
Add Answer to:
Create a program with threads that looks through a vary large array (100,000,000 elements) to find...
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
  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • 2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority...

    2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...

  • On the following code there is an error bc you are not reverting back to the...

    On the following code there is an error bc you are not reverting back to the original order after a sort. For the next sort you are passing the same reference variable to the next method. But that will point to the same (already sorted) array on the memory. Hence after the first sorting method, all three sorting methods are working on the already sorted array. Do the following : Just copy each data set to 4 different arrays -...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • I have a multithreaded java sorting program that works as follows: 1. A list of double...

    I have a multithreaded java sorting program that works as follows: 1. A list of double values is divided into two smaller lists of equal size 2. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice 3. The two sublists are then merged by a third thread merging thread that merges the two sublists into a single sorted list. SIMPLE EXECUTION >java SortParallel 1000 Sorting is done in 8.172561ms when...

  • Write merge method for mergeSort method, it takes the array of data, starting index of first...

    Write merge method for mergeSort method, it takes the array of data, starting index of first half, starting index of second half and end index of second half. please use the code below to test it public class A4Sort{ public static void mergeSort(int[] a, int l, int h){ if (l >= h) return; int mid = (h + l) / 2; mergeSort(a, l, mid); mergeSort(a, mid + 1, h); merge(a, l, mid + 1, h); } public static void main(String[]...

  • PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java,...

    PLEASE ANSWER #5AND #6, THE ANSWER FOR #3 AND #4 ARE ALREADY PROVIDED!!! 3 .Using Java, Write a computer program that prompts the user for one number, n for the number of items in the array to sort, and create and sort 1000 arrays of this size timing the run to get an average time to sort an array of this size. Then do the following: Initiate a variable running_time to 0 Create a for loop that iterates 1000 times....

  • Need help with this binary tree program. I will give a thumbs up for anybody who...

    Need help with this binary tree program. I will give a thumbs up for anybody who helps. Source Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class Trees { public static int[] prepareData(int[] data){ /* Data is prepared by inserting random values between 1 and data.length. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ ArrayList list = new ArrayList(); for (int i=0; i< data.length; i++) {...

  • the RollDie.java: import java.util.Random; import java.util.Scanner; /* * HEADER HERE !! */ public class RollDie {...

    the RollDie.java: import java.util.Random; import java.util.Scanner; /* * HEADER HERE !! */ public class RollDie { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // Step 1: Declare and initialize array and // initialize random number generator int[] rollValueCounts = new int[7]; Random randGen = new Random(); // Step 2: Determine the number of rolls System.out.print("How many times do you want to roll the die?"); System.out.print(" [Max value is 100] "); int numRolls = scnr.nextInt(); // TODO:...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

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