Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CSC 241 Lab 4 Given the following files (see Extra Files): IncreaseFactorial.java FactorialCounter.java TestFactorial.java Complete the parallelized version of the factorial method (n!). The multipleIncrement

CSC 241 Lab 4 Given the following files (see Extra Files): IncreaseFactorial.java FactorialCounter.java TestFactorial.java Complete the parallelized version of the factorial method (n!). The multipleIncrement method of the FactorialCounter class needs to be completed such that it uses the increment method. The result needs to be computed before it is returned from the factorialParallel method of TestFactorial class. Answer the following questions: 1) Remove the comments within the run method of the IncreaseFactorial class and observe the different threads executing with their various hold values. Do all four threads get to work all of the time? 2) Update the code to use only two threads. Does it run faster? Why, or why not? 3) Why does the parallelized method run slower than the serial version?

Need to code in Java.

TestFactorial:

//---------------------------------------------------------------------- // TestFactorial.java by Dale/Joyce/Weems Chapter 3 // // Repeatedly prompts user for a non-negative integer n. // Outputs n! calculated recursively and iteratively. // Uses a command line interface. //---------------------------------------------------------------------- package ch03.apps; import ch04.queues.*; import ch04.threads.*;

import java.util.Scanner;

public class TestFactorial { public static float factorialParallel(int n) throws InterruptedException{ float result; FactorialCounter c1 = new FactorialCounter(); FactorialCounter c2 = new FactorialCounter(); FactorialCounter c3 = new FactorialCounter(); FactorialCounter c4 = new FactorialCounter(); QueueInterface q; q = new SyncArrayBoundedQueue(n); for (int i = 2; i <= n; i++) q.enqueue(i); //Split up the numbers into 4 intervals //n must be greater than or equal to 4. Runnable r1 = new IncreaseFactorial(c1, q); Runnable r2 = new IncreaseFactorial(c2, q); Runnable r3 = new IncreaseFactorial(c3, q); Runnable r4 = new IncreaseFactorial(c4, q); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); Thread t3 = new Thread(r3); Thread t4 = new Thread(r4);

t1.start(); t2.start(); t3.start(); t4.start(); t1.join(); t2.join(); t3.join(); t4.join(); //compute result here.

return result; } public static float factorialParallelIdea(int n){ float result = 1; //Thread1 for(int i = n; i > (n - (n/4)); i--) result *= i; System.out.println("result for 100 to 76 = " + result); //Thread2 for(int i = n - (n/4); i > (n - (n/2)); i--) result *= i; System.out.println("result for 75 to 51 = " + result);

//Thread3 for(int i = n - (n/2); i > (n - (3*n/4)); i--) result *= i; System.out.println("result for 50 to 26 = " + result);

//Thread4 for(int i = n - (3*n/4); i > 0; i--) result *= i; System.out.println("result for 25 to 1 = " + result);

return result; } public static float factorialParallelRec(int n){ float result = 1; result = factorial(n) / factorial(3*n/4); System.out.println("result for 100 to 76 = " + result);

result *= factorial(3*n/4) / factorial(n/2); System.out.println("result for 75 to 51 = " + result);

result *= factorial(n/2) / factorial(n/4); System.out.println("result for 50 to 26 = " + result);

result *= factorial(n/4); System.out.println("result for 25 to 1 = " + result);

return result; } private static float factorial(int n) // Precondition: n is nonnegative // // Returns the value of "n!" { if (n == 0) return (float) 1.0; else return (n * factorial (n - 1)); } public static float factorial2(int n) // Precondition: n is nonnegative // // Returns the value of retValue: n! { float retValue = 1; while (n != 0) { retValue = retValue * n; n = n - 1; } return(retValue); } public static void main(String[] args) throws InterruptedException { Scanner scan = new Scanner(System.in); System.out.println("Factorial calculation test: "); int n = 0; long timeStart, timeEnd; while (n >= 0) { System.out.print(" Enter n (-1 to stop) > "); n = scan.nextInt(); if (n >= 0) { System.out.println(n + "! is"); System.out.println("\t recursive " + factorial(n)); System.out.println("\t iterative " + factorial2(n)); timeStart = System.nanoTime(); String s = String.format("%.0f", factorialParallelIdea(n)); timeEnd = System.nanoTime() - timeStart; System.out.println("\t iterative parallel idea " + s + " total time = " + timeEnd); timeStart = System.nanoTime(); s = String.format("%.0f", factorialParallel(n)); timeEnd = System.nanoTime() - timeStart; System.out.println("\t iterative parallel " + s + " total time = " + timeEnd); System.out.println("\t iterative parallel recursive " + factorialParallelRec(n)); } } } }

Increase Factorial:

package ch04.threads;

import ch04.queues.QueueInterface;

public class IncreaseFactorial implements Runnable { private FactorialCounter c; private QueueInterface q; public IncreaseFactorial(FactorialCounter c, QueueInterface q) { this.c = c; this.q = q; } public void run() { //String threadName = Thread.currentThread().getName(); //System.out.println("Hello " + threadName); int hold; int count; while (!q.isEmpty()) { hold = q.dequeue(); //System.out.println("hold = " + hold); c.multipleIncrement(hold); } } }

Factorial Counter:

package ch04.threads;

public class FactorialCounter { private float count; public FactorialCounter() { count = 1; }

public synchronized void increment() { count++; }

public synchronized void multipleIncrement(int times){ //complete this code } public synchronized float getCount(){return count;} }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions