PipedInputStream / PipedOutputStream with two threads problem....pls help

P

parag

Hi there,
I want to communicate two threads using pipe. Also I want to make
this process as cyclic process, currently it is non-cyclic
I am posting my code below
pls suggest something...........

//Pipe Writer class

import java.io.*;
public class Pipe_Writer extends Thread {
PipedWriter pWriter = new PipedWriter();
public Pipe_Writer() {}

public PipedWriter getPipedWriter() {
return pWriter;
}

public void run() {
Process p = null;
try {
p = Runtime.getRuntime().exec("cmd");
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(p.getOutputStream()));

BufferedReader reader2 = new BufferedReader(new
InputStreamReader(System.in));

String line;
String rd = "";
try {
rd = reader2.readLine();
System.out.println(rd);
} catch (IOException e1) {
e1.printStackTrace();
}

try {
// writer.write("dir\n"); //writing to process
writer.write(rd + "\n");
writer.flush();
while ((line = reader.readLine()) != null) {
System.out.println(line);
pWriter.write(line); //writing to pipe
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

=========================================

// Pipe reader class

import java.io.*;
public class Pipe_Reader extends Thread{
PipedReader pReader;

public Pipe_Reader(Pipe_Writer writer)throws IOException{
pReader = new PipedReader(writer.getPipedWriter());
}

public void run(){
try {
while(true){
System.out.println((char)pReader.read()); //Reading from pipe
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

=============================

// main method

public static void main(String[] args)throws IOException {
Pipe_Writer writer = new Pipe_Writer();
Pipe_Reader reader = new Pipe_Reader(writer);
writer.start();
reader.start();
}
====================================

pls HELP me!.....
 
G

Gordon Beaton

I want to communicate two threads using pipe. Also I want to make
this process as cyclic process, currently it is non-cyclic

Why don't you let the second thread read directly from the process'
stream itself? There's no need to complicate things with the
PipedStreams.

/gordon
 
P

parag

actually I want console like application in which one thread invokes
the process & executes the command, And other thread will captures the
output of that process & show it.
The programs I posted works for only one command at a time
(non-cyclic), I want it to execute every command given to it........
pls tell me what changes are needed
 
E

EJP

Gordon said:
Why don't you let the second thread read directly from the process'
stream itself? There's no need to complicate things with the
PipedStreams.

Agreed. And if there is a need, are you aware of
java.io.PipedInputStream and java.io.PipedOutputStream?
 
P

parag

Was there something wrong with Gordon's answer, or mine?


thanks for reply....
No, but stil it's not working.
I want one thread to take command get o/p and write it to pipe &
another to read it from pipe and display it, this works only for one
command and not give any o/p for second command, I have given the
source code. It must work for N number of commands

thanks,
parag
 
E

EJP

parag said:
No, but stil it's not working.

After what changes?
I want one thread to take command get o/p and write it to pipe &
another to read it from pipe and display it

Why? Why the pipe? How about Gordon's suggestion?
this works only for one command and not give any o/p for second command, I have given the
source code.

probably because our Pipe_Reader source code contains no check for EOF.

It's still far more complicated than it needs to be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top