Question

In: Computer Science

public class ProductThread { static class ProductThreads extends Thread{ private int begin, end; int[] v1, v2;...

public class ProductThread {

static class ProductThreads extends Thread{

private int begin, end;

int[] v1, v2;

long ris;

public ProductThreads(String name, int [] v1, int [] v2, int begin, int end) {

setName(name);

this.v1 = v1;

this.v2 = v2;

this.begin = begin;

this.end = end;

this.ris = 0;

}

public void run() {

System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started");

ris = 1;

for(int i = begin; i <= end; i++)

ris *= v1[i] * v2[i];

System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] completed");

}//run

public long getResult() {

return ris;

}

}//ProductThread

public static void main(String[] args) throws InterruptedException {

int [] a = {1,2,3,4,5,6,7,8,9,10};

int [] b = {1,2,3,4,5,6,7,8,9,19};

System.out.print("A = " );

print(a);

System.out.print("B = " );

print(b);

System.out.println();

//create threads

ProductThreads t0 = new ProductThreads("T0", a, b, 0, 2);

ProductThreads t1 = new ProductThreads("T1", a, b, 3, 5);

ProductThreads t2 = new ProductThreads("T1", a, b, 6, 9);

//start threads

t0.start();

t1.start();

t2.start();

//wait for completion of threads

t0.join();

t1.join();

t2.join();

//computation of final result

long result = 1;

System.out.println("T0 result= " + t0.getResult());

System.out.println("T1 result= " + t1.getResult());

System.out.println("T2 result= " + t2.getResult());

result *= t0.getResult() * t1.getResult() * t2.getResult();

System.out.println();

//final statement to be printed

System.out.println("Final Results = " + t0.getResult() + " * " + t1.getResult() + " * " + t2.getResult() + "= " + result);

}

static void print(int[] v) {

System.out.print("[");

for (int i = 0; i < v.length; i++) {

System.out.print(v[i] + " ");

System.out.println("]");

}

}

}

Using the same type of multithreading, I need to create a simple program that generates the factorial of a number, but splits up the calculation in multiple threads.

For instance, 5! = 5 * 4 * 3 * 2 * 1 so the threads would split the calculation up. Ex: T1 = 5 * 4 * 3 + T2 = 2 * 1

Solutions

Expert Solution

import java.util.Scanner;


public class ProductThread {

   static class ProductThreads extends Thread{

       private int begin, end;
       private long ris;
      
       public ProductThreads(String name,int begin, int end) {
           setName(name);
           this.begin = begin;
           this.end = end;
           this.ris = 1;
       }

       public void run() {
           System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] started");
           for(int i = begin; i >= end; i--)
               ris *= i;
           System.out.println("Thread " + Thread.currentThread().getName() + "[" + begin + "," + end + "] completed");
       }//run

       public long getResult() {
           return ris;
       }
   }//ProductThread

   public static void main(String[] args) throws InterruptedException {
  
       System.out.print("Enter the value for factorial : ");
      
       Scanner sc=new Scanner(System.in);
       int value;
       value=sc.nextInt();
       sc.close();
      
       //create threads
  
       // split value and set range
       // value to 2*value/3 , (2*value/3-1) to value/3 , (value/3-1) to 1
      
       ProductThreads t0 = new ProductThreads("T0", value, 2*value/3);
       ProductThreads t1 = new ProductThreads("T1",2*value/3-1, value/3);
       ProductThreads t2 = new ProductThreads("T2", value/3-1, 1);
      
       //start threads
      
       t0.start();
       t1.start();
       t2.start();
  
       //wait for completion of threads
      
       t0.join();
       t1.join();
       t2.join();
      
       //computation of final result
      
       long result = 1;
       System.out.println("T0 result= " + t0.getResult());
       System.out.println("T1 result= " + t1.getResult());
       System.out.println("T2 result= " + t2.getResult());
       result *= t0.getResult() * t1.getResult() * t2.getResult();
       System.out.println();
      
       //final statement to be printed
      
       System.out.println("Final Results = " + t0.getResult() + " * " + t1.getResult() + " * " + t2.getResult() + "= " + result);
   }
  
}


Related Solutions

import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T...
Using this BubbleSort implementation in Java: public class BubbleSort<T extends Comparable<T>> {    private static <T extends Comparable<T>>    void swap(T[] data, int index1, int index2)    {       T temp = data[index1];       data[index1] = data[index2];       data[index2] = temp;    }    public void sort(T[] data)    {       int position, scan;       for (position = data.length - 1; position >= 0; position--)       {          for (scan = 0; scan <= position - 1; scan++)          {...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
public class P2 { public static int F(int x[], int c) { if (c < 3)...
public class P2 { public static int F(int x[], int c) { if (c < 3) return 0; return x[c - 1] + F(x, c - 1); } public static int G(int a, int b) { b = b - a; a = b + a; return a; } public static void main(String args[]) { int a = 4, b = 1; int x[] = { 3, 1, 4, 1, 5 }; String s = "Problem Number 2"; System.out.println(x[2 +...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5; // initial array size    //DO NOT CHANGE this, it is set to 5 to make sure your code    //will pass all the tests and works with no issue.       // add all data fields which are required    /**    * ArrayBasedDeque() constructs an empty deque.    */    public ArrayBasedDeque(){        //complete    }       /**    *...
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT