Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
parag - 24 Aug 2006 08:28 GMT
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!.....
Gordon Beaton - 24 Aug 2006 09:05 GMT
>   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

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

parag - 24 Aug 2006 09:23 GMT
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
parag - 28 Aug 2006 06:22 GMT
????????????????????????
EJP - 28 Aug 2006 08:21 GMT
> ????????????????????????

Was there something wrong with Gordon's answer, or mine?
parag - 28 Aug 2006 08:55 GMT
> 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
EJP - 28 Aug 2006 09:06 GMT
>>Was there something wrong with Gordon's answer, or mine?
>
> 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.
EJP - 24 Aug 2006 09:27 GMT
> 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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.