【发布时间】:2014-03-24 15:53:04
【问题描述】:
我正在学习 java PipedInputStream/PipeOutputStream 。
我想阅读标准输入(下面的 'Source' 类)并将其重定向到一个进程(这里是 'grep A'),即 Grep 的输出将被重定向到 System.out。
为了在 grep 之后使用 stdout 和 stderr,我还创建了一个类 CopyTo 来将输入流重定向到输出流。
import java.io.*;
class Test
{
private static class Source
implements Runnable
{
private PipedOutputStream pipedOutputStream=new PipedOutputStream();
private InputStream in;
Source(InputStream in) throws IOException
{
this.in=in;
}
@Override
public void run()
{
try
{
int c;
while((c=this.in.read())!=-1)
{
pipedOutputStream.write(c);
}
pipedOutputStream.flush();
pipedOutputStream.close();
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
private static class Grep
implements Runnable
{
private PipedInputStream pipeInPipedInputStream;
public Grep(Source src) throws IOException
{
this.pipeInPipedInputStream=new PipedInputStream(src.pipedOutputStream);
}
@Override
public void run()
{
try {
Process proc=Runtime.getRuntime().exec(new String[]{
"/bin/grep",
"A"});
OutputStream os=proc.getOutputStream();
Thread t1=new Thread(new CopyTo(proc.getErrorStream(),System.err));
Thread t2=new Thread(new CopyTo(proc.getInputStream(),System.out));
t1.start();
t2.start();
int c;
while((c=this.pipeInPipedInputStream.read())!=-1)
{
os.write((char)c);
}
t1.join();
t2.join();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
private static class CopyTo implements Runnable
{
private InputStream in;
private OutputStream out;
CopyTo(InputStream in,OutputStream out)
{
this.in=in;
this.out=out;
}
@Override
public void run() {
try {
int c;
while((c=in.read())!=-1)
{
out.write(c);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
try
{
Source src=new Source(System.in);
Thread t1=new Thread(src);
Thread t2=new Thread(new Grep(src));
t1.start();
t2.start();
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
但是,编译和运行程序不会产生任何输出(并且程序被冻结)。
$ javac Test.java && echo -e "A\nT\nG\nC" | java Test
我哪里错了?谢谢。
【问题讨论】:
标签: java multithreading process pipe java.util.concurrent