Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

:Java Programming Write a class Summer that computes the sum of numbers from 1 to n in parallel using multiple concurrent threads. The class interface

:Java Programming

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 wait/notify 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

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_2

Step: 3

blur-text-image_3

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

DB2 Universal Database V7.1 Application Development Certification Guide

Authors: Steve Sanyal, David Martineau, Kevin Gashyna, Michael Kyprianou

1st Edition

0130913677, 978-0130913678

More Books

Students also viewed these Databases questions

Question

LO5.2 Discuss government failure and explain why it happens.

Answered: 1 week ago