On the program below. The program run in infinite loop and asked the user for 3 variable forever. I tried to change the while loop to be for loop, but it end up error. How do i make it run once? Like ask for 3 variable then sold the problem that it.
// Example showing how to pass data from one thread to another // through a pipe, for the Quadratic Equation Solver. import java.util.*; import java.io.*; public class EquPiping { public static void main(String args[]) { try { PipedOutputStream pout1 = new PipedOutputStream(); PipedInputStream pin1 = new PipedInputStream(pout1); PipedOutputStream pout2 = new PipedOutputStream(); PipedInputStream pin2 = new PipedInputStream(pout2); Producer prod = new Producer(pout1); Filter filt = new Filter(pin1, pout2); Consumer cons = new Consumer(pin2); cons.start(); prod.start(); filt.start(); } catch (IOException e) { } } } //////////////////////////////////////////////////////////////// // This thread reading coefficients from keyboard and passing // // them through a pipe to the Solver. // class Producer extends Thread { private DataOutputStream out; private Random rand = new Random(); private double num = 0.0; BufferedReader IOobj = new BufferedReader(new InputStreamReader(System.in)); public Producer(OutputStream os) { out = new DataOutputStream(os); } public void run() { ///////////////////////////////////////////////////////// WAS trying to change the while loop below. while (true) { try { System.out.print("(1) >>>Please enter next coefficient: "); num = Double.parseDouble(IOobj.readLine()); System.out.println("(1) Passing coefficient into a pipe."); out.writeDouble(num); out.flush(); // Short sleeping time may cause output buffering problems. // sleep(Math.abs(rand.nextInt() % 1000)); sleep(1000); } catch (Exception e) { System.out.println("Error1: " + e); } } } } ///////////////////////////////////////////////////////////////// // This thread receiving coefficients from a pipe, solving the // // equation, and passing roots to the third thread for display.// class Filter extends Thread { private DataInputStream in; private DataOutputStream out; public Filter(InputStream is, OutputStream os) { in = new DataInputStream(is); out = new DataOutputStream(os); } public void run() { for (;;) { try { double a = in.readDouble(); double b = in.readDouble(); double c = in.readDouble(); System.out.println("\t(2) >>>Root calculating thread..."); if (b*b-4*a*c >= 0.0) { double root1 = (-b - Math.sqrt(b*b-4*a*c))/(2.0*a); double root2 = (-b + Math.sqrt(b*b-4*a*c))/(2.0*a); out.writeDouble(root1); out.writeDouble(root2); } else { out.writeDouble(0.0); out.writeDouble(0.0); } System.out.println("\t(2) Root solver passed..."); } catch (IOException e) { System.out.println("Error2: " + e); } } } } ///////////////////////////////////////////////////////////////// // This thread receiving roots from a pipe and displating them.// class Consumer extends Thread { private DataInputStream in; public Consumer(InputStream is) { in = new DataInputStream(is); } public void run() { for (;;) { try { // Verification that the rootss are actually passed. double avg1 = in.readDouble(); System.out.println("\t\t(3) >>>Consuming the roots..."); System.out.println("\t\t(3) Displaying root #1: " + avg1); double avg2 = in.readDouble(); System.out.println("\t\t(3) Displaying root #2: " + avg2); } catch (IOException e) { System.out.println("Error3: " + e); } } } } ////////////////////////////////////////////////////////////////
I think I found the problem, but will wait if there any opinion.