is it possible for One PipeInputStream to stream out to two PipeOutputStream? I am trying to get the third thread to from Pipe 2 to read the first thread from Pipe 1.
The class displayInfo is the problem for me below. It read 3 variable TWICE, before it can print out the results on the second time. Please help me fix 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 assign2 { public static void main(String args[]) { try { PipedOutputStream out1 = new PipedOutputStream(); PipedInputStream in1 = new PipedInputStream(out1); PipedOutputStream out2 = new PipedOutputStream(); PipedInputStream in2 = new PipedInputStream(out2); readCoeffs prod = new readCoeffs(out1); calRoots filt = new calRoots(in1, out2); displayInfo cons = new displayInfo(in1, in2); cons.start(); prod.start(); filt.start(); } catch (IOException e) { } } } class readCoeffs extends Thread { private DataOutputStream out; private int count = 1; double[] readers = new double[3]; BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); public readCoeffs(OutputStream os) { out = new DataOutputStream(os); } public void run() { while (true) { try { if (count > 3) { count = 1; } for (int i = 0; i < 3; i++) { System.out.print("Please enter coefficient #" + count + ": "); readers[i] = Double.parseDouble(input.readLine()); System.out.println("Passing coefficient #" + count + " into a pipe."); count++; out.writeDouble(readers[i]); } out.flush(); sleep(1000); } catch (Exception e) { System.out.println("readCoeffs ERROR: " + e); } } } } class calRoots extends Thread { private DataInputStream in; private DataOutputStream out; double[] readersin = new double[3]; double[] roots = new double[2]; public calRoots(InputStream is, OutputStream os) { in = new DataInputStream(is); out = new DataOutputStream(os); } public void run() { for (;;) { try { for (int i = 0; i < 3; i++) { readersin[i] = in.readDouble(); } double a = readersin[0]; double b = readersin[1]; double c = readersin[2]; System.out.println("\t(2) >>>Root calculating thread..."); if (b * b - 4 * a * c >= 0.0) { roots[0] = (-b - Math.sqrt(b * b - 4 * a * c)) / (2.0 * a); roots[1] = (-b + Math.sqrt(b * b - 4 * a * c)) / (2.0 * a); out.writeDouble(roots[0]); out.writeDouble(roots[1]); out.flush(); } else { out.writeDouble(0.0); out.writeDouble(0.0); } System.out.println("\t(2) Root solver passed..."); } catch (IOException e) { System.out.println("calRoots ERROR: " + e); } } } } class displayInfo extends Thread { private DataInputStream in1; private DataInputStream in2; double[] readersin = new double[3]; double[] avg = new double[2]; public displayInfo(InputStream is1, InputStream is2) { in1 = new DataInputStream(is1); in2 = new DataInputStream(is2); } public void run() { for (;;) { try { for (int i = 0; i < 3; i++) { readersin[i] = in1.readDouble(); } double a = readersin[0]; double b = readersin[1]; double c = readersin[2]; for (int i = 0; i < 2; i++) { avg[i] = in2.readDouble(); } double root1 = avg[0]; double root2 = avg[1]; System.out.println("For coefficients"); System.out.println("a: " + a + ", b: " + b + ", c: " + c); System.out.println("\t\t(3) >>>Consuming the roots..."); System.out.println("\t\t(3) Displaying root #1: " + root1); System.out.println("\t\t(3) Displaying root #2: " + root2); } catch (IOException e) { System.out.println("Error3: " + e); } } } }