Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: Run the program and observe the output (but I get errors and not sure why) BusinessLogic.java import java.util.Scanner; public class BusinessLogic { int num1,

JAVA: Run the program and observe the output (but I get errors and not sure why)

BusinessLogic.java

import java.util.Scanner; public class BusinessLogic { int num1, num2 = 1; int total = 0; prod = 1; public void reader(){ // We want reader to run on it's own thread Scanner scanner = new Scanner(System.in); // read from the keyboard while (true){ // infinite loop to keep reading num1 = scanner.nextInt(); num2 = num1; } }// reader Public void total(){ while (true){ // infinite loop to keep reading try { Thread.currentThread().Sleep (3 * 1000); // to do it every 3 seconds } catch(InterruptedException ie) {} total += num1; // now we can access num, so we can use it here num1 = 0; System.out.println("Total so far: " + total): } }// total Public void product(){ while (true){ // infinite loop to keep reading try { Thread.currentThread().Sleep (5 * 1000); // to do it every 5 seconds } catch(InterruptedException ie) {} prod *= num2; // now we can access num, so we can use it here num2 = 1; System.out.println("Product so far: " + prod): } }// product - we need to call this function from the new thread }// class BusinessLogic

ReaderThread.java

import java.util.Scanner; import java.util.concurrent.*; public class ReaderThread implement Runnable { int num; // made num a globle variable BusinessLogic bl; public ReaderThread(BusinessLogic bl){ this.bl = bl; } public void run(){ bl.reader(); } public static void main (string[] args) { // we need to actually create two threads // need to create BusinessLogic first then pass it to the thread BusinessLogic bl = new BusinessLogic(){ Thread readerThread = new Thread(new ReaderThread(bl)); Thread totalThread = new Thread(new TotalThread(bl)); Thread prodThread = new Thread(new ProductThread(bl)); totalThread.start(); prodThread.start(); readerThread.start(); } }

TotalThread.java

// TotalThread // Not reading so don't need the following 2 imports // import java.util.Scanner; // import java.util.concurrent; public class TotalThread implement Runnable { int total =0; // total to keep track. Initialize BusinessLogic bl; public TotalThread(BusinessLogic bl){ this.bl = bl; } public void run(){ bl.total(); } } // problem? the total and the num from user are in different classes // solution - call the function inside or run. Create another - BusinessLogic 

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

More Books

Students also viewed these Databases questions

Question

LO 10-5 Differentiate among Theory X, Theory Y, and Theory Z.

Answered: 1 week ago

Question

Differentiate tan(7x+9x-2.5)

Answered: 1 week ago

Question

Explain the sources of recruitment.

Answered: 1 week ago

Question

Differentiate sin(5x+2)

Answered: 1 week ago

Question

Compute the derivative f(x)=1/ax+bx

Answered: 1 week ago