Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to use concurrency of a series problem in java?

Observe the Series.java and SeriesSolver.java classes that have been provided. The Series class represents a series as characterized by an equation for the nth element (using the Java BigDecimal class). The SeriesSolver.java class provides methods for computing the partial sum of a given Series object from one value of n (a) to another (b). A single-threaded implementation computeSum(Series series, int a, int b); has been provided for you.

Heading ##This method computes the sum using a

for loop. Your task is to implement an overload of this method: computeSum(Series series, int a, int b, int threads); that performs the same task but using threads number of threads. Your code should split the problem into equal-sized sub-problems and then use these answers to generate the overall solution. 1) Implement the computeSum method above. 2) Write a JUnit test for your method comparing the results to the results of the single- threaded implementation. Use the predefined Series objects provided in the Series class.

import java.math.BigDecimal; import java.math.MathContext;

/** * A naive implementation of a Series, as characterized * by an equation for its nth term. */ public interface Series {

/**
 * Computes the nth value of the series.
 *
 * @param n The nth term.
 */
public BigDecimal computeValue(int n);

/*
 * Sum from 0 -> Infinity = 2. This will be slower than the PI Series below due to the call to pow().
 *
 * f(n) = 1/2^n
 */
public static final Series TWO = new Series(){
    @Override
    public BigDecimal computeValue(final int n){
        return BigDecimal.ONE.divide(new BigDecimal(2).pow(n));
    }
};

/*
 * Value from 0 -> Infinity = PI.
 *
 * f(n) = 1/(2n+1)
 */
public static final Series PI = new Series(){
    private final BigDecimal FOUR = new BigDecimal(4);
    @Override
    public BigDecimal computeValue(final int n){
        if (n % 2 == 0){
            return FOUR.divide(new BigDecimal(2*n+1),MathContext.DECIMAL128);
        }
        else {
            return FOUR.divide(new BigDecimal(2*n+1),MathContext.DECIMAL128).negate();
        }
    }
};

}//end of series

import java.math.BigDecimal;

/** * Utility class that computes approximations of series. */ public class SeriesSolver {

/**
 * Computes the series sequentially using only one thread.
 *
 * @param series The Series to solve.
 * @param a The min n value to iterate to.
 * @param b The max n value to iterate to.
 * @return The sum of the series from a to b.
 */
private static BigDecimal computeSum(final Series series, final int a, final int b){
    if (a > b){
        throw new IllegalArgumentException("a cannot be greater than b.");
    }
    BigDecimal sum = new BigDecimal(0);
    for (int n = a; n <= b; n++){
        sum = sum.add(series.computeValue(n));
    }
    return sum;
}

/**
 * Computes the series using multiple threads.
 *
 * @param series The Series to solve.
 * @param a The min n value to iterate to.
 * @param b the max n value to iterate to.
 * @param threads The number of threads to use (can be less if b-a is small).
 * @return The sum of the series from a to b.
 */
private static BigDecimal computeSum(final Series series, final int a, final int b, final int threads){
    if (a > b){
        throw new IllegalArgumentException("a cannot be greater than b.");
    }
    if (threads <= 0){
        throw new IllegalArgumentException("Number of threads must be positive.");
    }
    //Fill in this implementation.
    return null;
}

}//end of series solver

Comments