In: Computer Science
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
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);
}
}