Question: Create an multithread program in java to calculate the summation of an array. The structure of the program is below: The program should have a
Create an multithread program in java to calculate the summation of an array. The structure of the program is below:
The program should have a class called SynchronizationDemo. The main main method should generate an integer array whose 100 elements are 1,... 100. Then Repeat the following 100 times. Make two threads for the class called Sum and use it to calculate the summation of the 100 elements. The first thread should add the first 50 elements to the summation , and the second thread should add the last 50 elements to the summation. Display the summation.
Sum is a inner class which implements Runnable interface.The run method should perform these steps:
Calculate the total of the elements from index start to index end
Call Thread.sleep(5) to pause the thread
Add the total to summation
// Please document the program
// Here is an sample output of the program
With synchronization Loop = 0 sum = 5050 Loop = 1 sum = 5050 Loop = 2 sum = 5050 Loop = 3 sum = 5050 Loop = 4 sum = 5050 Loop = 5 sum = 5050 Loop = 6 sum = 5050 Loop = 7 sum = 5050 Loop = 8 sum = 5050 Loop = 9 sum = 5050 Loop = 94 sum = 5050 Loop = 95 sum = 5050 Loop = 96 sum = 5050 Loop = 97 sum = 5050 Loop = 98 sum = 5050 Loop = 99 sum = 5050
Without synchronization Loop = 0 sum = 1326 Loop = 1 sum = 5050 Loop = 2 sum = 1326 Loop = 3 sum = 5050 Loop = 4 sum = 5050 Loop = 5 sum = 5050 Loop = 6 sum = 3724 Loop = 7 sum = 5050 Loop = 8 sum = 5050 Loop = 94 sum = 5050 Loop = 95 sum = 5050 Loop = 96 sum = 5050 Loop = 97 sum = 5050 Loop = 98 sum = 5050 Loop = 99 sum = 5050
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
