Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(JAVA) Modify the above code with the following requirements: - The above code only reads one integer input from the user on Console. You need

(JAVA) Modify the above code with the following requirements:

- The above code only reads one integer input from the user on Console. You need to change the code to read a sequence of integer inputs and store them into an array, i.e., arrUpper

- Add code to prompt the user to enter the number of threads in the pool. Then, read the user input into an integer variable such as num_thread_pool

- Besides the newSingleThreadExecutor() method, the ExecutorService class supports another method to create a fixed-size thread pool: newFixedThreadPool(int size). Now, modify the above code to change from newSingleThreadExecutor() to newFixedThreadPool(). You need to use the above num_thread_pool variable to pass to this method.

- Finally, modify the two lines of code inside the try block to use a for loop to submit a new Summation task instance of each element in the array arrUpper

c. Run your modified code with the same numbers of the input integers but changing the input number of threads in the pool. Compare the execution time with such a different number of threads in the pool and discuss your findings.

image text in transcribed

- Below is a sample output of the modified program to give you an idea of how to modify the given code correctly:

image text in transcribed

import java.util.Scanner; import java.util.concurrent.*; class Summation implements Callable { private int upper; public Summation(int upper) { this.upper = upper; } /* The thread will execute in this method */ public Integer call() { int sum = 0; for (int i = 1; i result = pool.submit(new Summation (upper)); System.out.println("sum = + result.get()); } catch (InterruptedException | ExecutionException ie) { System.out.println("Error executing the task!"); } long endtime = System. nanoTime(); // get execution time long timeElapsed = endTime - startTime; System.out.println("Total execution time: timeElapsed / 1000000 + ms"); pool.shutdown(); } } + Enter the upper integer array: 10000 20000 30000 40000 Enter the number of threads in the pool: 2 Sum of 1 to 10000 = 50005000 Sum of 1 to 20000 = 200010000 Sum of 1 to 30000 = 450015000 Sum of 1 to 40000 800020000 Total execution time: 4 ms import java.util.Scanner; import java.util.concurrent.*; class Summation implements Callable { private int upper; public Summation(int upper) { this.upper = upper; } /* The thread will execute in this method */ public Integer call() { int sum = 0; for (int i = 1; i result = pool.submit(new Summation (upper)); System.out.println("sum = + result.get()); } catch (InterruptedException | ExecutionException ie) { System.out.println("Error executing the task!"); } long endtime = System. nanoTime(); // get execution time long timeElapsed = endTime - startTime; System.out.println("Total execution time: timeElapsed / 1000000 + ms"); pool.shutdown(); } } + Enter the upper integer array: 10000 20000 30000 40000 Enter the number of threads in the pool: 2 Sum of 1 to 10000 = 50005000 Sum of 1 to 20000 = 200010000 Sum of 1 to 30000 = 450015000 Sum of 1 to 40000 800020000 Total execution time: 4 ms

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

More Books

Students also viewed these Databases questions

Question

For the circuit in Fig. 7.127, calculate i(t) if i(0) = 0. 3 J2 6

Answered: 1 week ago