Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: PipeInputStream & PipeOutputStream

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PipeInputStream & PipeOutputStream

    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);
    			}
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: PipeInputStream & PipeOutputStream

    Can you post the contents of the console from when you execute the program that shws your input and the programs output. Add some comments to the post to show what the output should be or to show where the error is.

    To make for easier testing, put the input into the program. Change the StringReader on the following line to be the program's input:
           StringReader sr = new StringReader("3\n34\n56\n78\n12\n23\n34\n");
    	BufferedReader input = new BufferedReader(sr); //new InputStreamReader(System.in));
    Last edited by Norm; September 25th, 2012 at 04:56 PM.
    If you don't understand my answer, don't ignore it, ask a question.