However, it is running on the thread, but in the wrong way. It needs to run continuously followed by how many threads I choose based on the start and end value to perform the calculation. For example,  if I choose the start value to be 1 and the end value to be 10 as I choose to run 2 threads, the first thread runs 1 to 5 factorials and the second thread runs 6 to 10 factorial.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need to add user input to allow the number of threads to be run and a start and an end value to print the lists of calculations. 
However, it is running on the thread, but in the wrong way. It needs to run continuously followed by how many threads I choose based on the start and end value to perform the calculation.
For example,  if I choose the start value to be 1 and the end value to be 10 as I choose to run 2 threads, the first thread runs 1 to 5 factorials and the second thread runs 6 to 10 factorial. 
Or if I want to run 2 threads still, but from 1 to 100 factorial and it will halve into each thread.

For example, if I choose to run 2 threads and input the start number to be 1 and the end number to be 10, it will halve into each thread.
The following output:
(User input)
Enter the number of threads to generate:
2
Enter the first number:1
Enter the second number:10
(Output based on user input)
THREAD 1:
Factorial of 1 is: 1 
Factorial of 2 is: 2 
Factorial of 3 is: 6 
Factorial of 4 is: 24
Factorial of 5 is: 120
THREAD 2:
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800


Java Program:

import java.util.Scanner;
class Fact extends Thread {
    static int start;
    static int end;

    //constructor
    Fact(int sN, int eN) {
      Fact.start = sN;
      Fact.end = eN;    
    }
    public static void calFactorial() {
        try {
            if (start < 0 || end < 0 || start > end) {
                return;
            }
        } catch (Exception e) {
        }
    }
   
    public void run() {
        for (int i = start; i <= end; i++) {
            int fact = 1;
            for (int j = 1; j <= i; j++) {
                fact = fact * j;
            }
            System.out.println("Factorial of " + i + " is: " + fact);
    }
}

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

            int num;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the number of threads to generate:");
            num = sc.nextInt();
            System.out.print("Enter the first number:");
            start = sc.nextInt();
            System.out.print("Enter the second number:");
            end = sc.nextInt();
            for (int i = 0; i < num; i++) {
                Fact fac = new Fact(start,end);
                fac.start();
                fac.join();
                sc.close();

               

            }
        }
    }
}


As of now, it is only running on the thread but it is repeating the start and end values.
For example, if I choose to run 2 threads and I choose the start number to be 1 and the end number to be 10, the output will be:
Enter the number of threads to generate:
2
Enter the first number:1
Enter the second number:10
Factorial of 1 is: 1 
Factorial of 2 is: 2 
Factorial of 3 is: 6 
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800
Factorial of 1 is: 1
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800



Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of Threads
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education