Question: a. (JAVA) Explain in a few sentences (1-5) the behavior of the following program (what does the program trying to do, input, and output?). You

a. (JAVA) Explain in a few sentences (1-5) the behavior of the following program (what does the program trying to do, input, and output?). You may want to run the above code with a big enough input integer to check the execution time.

a. (JAVA) Explain in a few sentences (1-5) the behavior of the

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

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

following program (what does the program trying to do, input, and output?).

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!