Answered step by step
Verified Expert Solution
Question
1 Approved Answer
JAVA CODE: 9.1 write a class Summer that computes the sum of numbers from 1 to n in parallel using multiple concurrent threads The class
JAVA CODE:
9.1 write a class Summer that computes the sum of numbers from 1 to n in parallel using multiple concurrent threads The class interface has a public method: *@param n the upper limit @param k the number of threads @return the sum 1 2 +... +n computed with k threads int sum(int n, int k); The sum 1+2+ +n is broken down in k intervals: [0, n/k], [n/k+1, 2*n/k], [2*n/k+1, 3*n/k], , [(k-1)*n/k, n] A "worker"thread with index j (from 0 to k-1) computes the partial sum of the interval [j*n/k, (j+1)*n/k]. After all worker threads finish summing their interval, the main thread adds all partial sums and returns the total sum. Make sure it works. Test against the formula sum-n* (n+1)/2. Use either ReentrantLock with Condition or use the Java object lock with waitotify and "synchronized" methods/blocks (preferred solution). Points will be deducted if race conditions are possible
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started