Question

Develop a multithreaded app that can find the integer in the range 1 to 10000 that...

Develop a multithreaded app that can find the integer in the range 1 to 10000 that has the largest number of divisors. It should print that integer as well as the time spent to finish the calculation. use Java and Any API library.

Please provide the code and answer the following

1. How long it took your app to finish the job when using 1 thread?

2. How many threads your machine can run simultaneously?

3. How long it took your app to finish the job when using all available threads?

4. Did you use task or data parallelization for this program? Why?

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

b) It depends upon CPU thread handling mechanism and JVM installed.

c) around 80ms for 8 threads

d) Yeah, splitted the task into multiple parts.

Code

public class Temp {

    

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

        int N=4;  //number of threads

        Factorize[]f = new Factorize[N];  

        long start = System.currentTimeMillis();  //timer start

        for(int i=0;i<N;i++){

            f[i] = new Factorize(i*10000/N, (i+1)*10000/N);  //assign each hhtread task

            f[i].start();

        }

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

            f[i].join();

        long end = System.currentTimeMillis();  //stop timer

        int result=-1, maxFactor=0;

        for(int i=0;i<N;i++){   //gather maximum result

            if(f[i].maxFactor>maxFactor){

                maxFactor = f[i].maxFactor;

                result = f[i].result;

            }

        }

        System.out.println("Number: "+result);

        System.out.println("Factors: "+maxFactor);

        System.out.println("Number of Threads: "+N);

        System.out.println("Execution time (in ms): "+(end-start));


    }

}

class Factorize extends Thread{

    int lower, upper, result, maxFactor;

    Factorize(int l,int u){  //range of numbers to find answer

        lower = l ;

        upper = u ;

        maxFactor=0;

    }

    int numOfFactor(int n){  //factor of 1 number

        int count=0;

        for(int i=1;(i)<=n;i++)

            if(n%i==0)

                count++;

        return count;

    }

    @Override

    public void run() {

        for(int i=lower;i<=upper;i++){

            int f = numOfFactor(i);

            if(f>maxFactor){

                maxFactor=f;

                result=i;

            }

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Develop a multithreaded app that can find the integer in the range 1 to 10000 that...
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
  • Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the ...

    Sorting Threads Assignment Overview Write a multithreaded sorting program in Java which uses the merge sort algorithm. The basic steps of merge sort are: 1) divide a collection of items into two lists of equal size, 2) use merge sort to separately sort each of the two lists, and 3) combine the two sorted lists into one sorted list. Of course, if the collection of items is just asingle item then merge sort doesn’t need to perform the three steps,...

  • Please I need java code for this question with output: 4.19) The Fibonacci sequence is the...

    Please I need java code for this question with output: 4.19) The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8, .... Formally, it can be expressed as: f ib0 = 0 f ib1 = 1 f ibn = f ibn−1 + f ibn−2 Write a multithreaded program that generates the Fibonacci sequence using either the Java, Pthreads, or Win32 thread library. This program should work as follows: The user will enter on the command...

  • 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...

  • Description In this homework, you are asked to implement a multithreaded program that will allow ...

    Description In this homework, you are asked to implement a multithreaded program that will allow us to measure the performance (i.e, CPU utilization, Throughput, Turnaround time, and Waiting time in Ready Queue) of the four basic CPU scheduling algorithms (namely, FIFO, SJE PR, and RR). Your program will be emulating/simulating the processes whose priority, sequence of CPU burst time(ms) and I'O burst time(ms) will be given in an input file. Assume that all scheduling algorithms except RR will be non-preemptive,...

  • 1. The first task in this assignment creates the pid manager whose implementation can simply be...

    1. The first task in this assignment creates the pid manager whose implementation can simply be a single class. Of course, you can create any other classes you might need to implement the pid manager. You may use any data structure of your choice to represent the availability of process identifiers. One strategy adopts Linux’s approach of a bitmap in which a value of 0 at position i indicates that a process id of value i is available and a...

  • Do the following project: Following is the file to be programmed in Linux kernel. Run this...

    Do the following project: Following is the file to be programmed in Linux kernel. Run this program. Include the screenshot of the results. Multi threaded Sorting Application Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sub list using a sorting algorithm of your choice. The two sub lists are then merged by a third thread—a...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

  • Question III This question carries 20% of the marks for this assignment. Given the following mix...

    Question III This question carries 20% of the marks for this assignment. Given the following mix of tasks, task lengths and arrival times, compute the completion [5 marks and response time time from the arrival to the finish time) (5 marks for each task, along with the average response time for the FIFO. RR and SJF algorithms. Assume a time slice of 10 milliseconds and that all times are in milliseconds. You are kindly asked to provide the Gantt Chart...

  • From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integ

    From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integers starting from var and ending with 1. For example, when var is 6, this list of integers is 6,3,5,8,4,2,1, which has a length of 7 because this list contains 7 integers (call this list the Collatz list for 6). Write a C or C++...

  • 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...

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