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

60.1K

Verified Solution

Question

Programming

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, intbegin, 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) throwsInterruptedException {

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 simpleprogram that generates the factorial of a number, but splits up thecalculation in multiple threads.

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

Answer & Explanation Solved by verified expert
4.3 Ratings (808 Votes)
import javautilScannerpublic class ProductThread static class ProductThreads extends Thread private int begin end private long ris public ProductThreadsStringnameint begin int end setNamename thisbegin begin thisend end thisris 1 public void run    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students