Question

In: Computer Science

In this programming assignment, you are asked to write a multi-threaded program using JAVA to compute...

  1. In this programming assignment, you are asked to write a multi-threaded program using JAVA to compute the sum of an arithmetic series from 1 to 200 using pthreads.   The main thread is responsible for creating two child threads

  1. The first child thread should compute the partial sum of the series from 1 to 100 and the second computes the partial sum of another series from 101 to 200.

  1. The main thread should accumulate the partial sums computed by the child threads to obtain the final sum and then print out the results, including the partial sums computed by each child thread and the final sum accumulated by the main thread.

Solutions

Expert Solution

// ArithmeticSeries class that implements Runnable interface
// and calculate sum from start to end by taking parameters through constructor
class ArithmeticSeries implements Runnable{
private volatile int sum = 0;
private int start, end;
// get start and end
public ArithmeticSeries(int s, int e){
    start = s;
    end = e;
}

// get sum
@Override
public void run() {
    calculateSum();
}

// calculate sum by looping from start to end and adding it to sum
public void calculateSum() {
    try{
      for(int i = start; i <= end; i++){
        sum = sum + i;
      }
    }catch (Exception e){
    }
}

// return calculated result sum
public int getSum(){
    return sum;
}
}


public class ArithmeticSeriesTest{
public static void main(String args[]){
    // create two ArithmeticSeries object by passing 1-100 & 101-200 to each object
    ArithmeticSeries as1 = new ArithmeticSeries(1, 100);
    ArithmeticSeries as2 = new ArithmeticSeries(101, 200);

    // create two threads from main thread by passing ArithmeticSeries object
    Thread t1 = new Thread(as1);
    Thread t2 = new Thread(as2);
    // start executing threads
    t1.start();
    t2.start();

    // join threads so that each thread can report main thread about exit
    try{
      t1.join();
      t2.join();
    }catch(Exception e){}

    // collect result from both threads and compute final sum
    int as1_sum = as1.getSum();
    int as2_sum = as2.getSum();
    int total = as1_sum + as2_sum;
    System.out.println("sum:- " + total);

}
}


Related Solutions

For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a file without the file becoming corrupted. To do this, your program will create three threads which write strings to the same file. Each thread will randomly write a selection of strings to the file at random intervals. When finished, the file will contain all the strings written correctly to the file. You may use mutexes, semaphores, or a monitor your write on your own....
using java For this assignment, you will write a program that guesses a number chosen by...
using java For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to choose a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods...
Java Programming II Homework 2-1 In this assignment you are being asked to write some methods that operate on an array of int values. You will code all the methods and use your main method to test your methods. Your class should be named Array Your class will have the following methods (click on the method signatures for the Javadoc description of the methods): [ https://bit.ly/2GZXGWK ] public static int sum(int[] arr) public static int sum(int[] arr, int firstIndex, int...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester.    The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
Program: Java Write a Java program using good programming principles that will aggregate the values from...
Program: Java Write a Java program using good programming principles that will aggregate the values from several input files to calculate relevant percentages and write the values to an output file. You have been tasked with reading in values from multiple files that contains different pieces of information by semester. The Department of Education (DOE) would like the aggregate values of performance and demographic information by academic year. A school year begins at the fall semester and concludes at the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT