//:PipedStreamTest.java
import java.io.*;public class PipedStreamTest {
public static void main(String[] args) {
Thread t1=new SenderThread();
Thread t2=new ReceiveThread();
PipedOutputStream out=t1.getPipedOutputStream();
PipedInputStream in=t2.getPipedInputStream();
out.connect(in);
t1.start();
t2.start();
}
}
class SenderThread extends Thread {
private PipedOutputStream out=new PipedOutputStream();
PipedOutputStream getPipedOutputStream() {
return out;
}
public void run() {
out.write("www.xmjobs.com".getBytes());
out.close();
}
}
class ReceiveThread extends Thread {
private PipedInputStream in=new PipedInputStream();
PipedInputStream getPipedInputStream() {
return in ;
}
public void run() {
byte[] buf=new byte[1024];
int len=in.read(buf);
System.out.println(new String(buf,0,len));
in.close();
}
}

解决方案 »

  1.   

    你是什么问题呀?两个Stream不能通信?你应该让线程sleep()一下,否则太忙了,管道都塞满了.
      

  2.   

    // :PipedStreamTest.javaimport java.io.*;public class PipedStreamTest {
    public static void main(String[] args) {
    SenderThread t1 = new SenderThread();
    ReceiveThread t2 = new ReceiveThread();
    PipedOutputStream out = t1.getPipedOutputStream();
    PipedInputStream in = t2.getPipedInputStream();
    try {
    out.connect(in);
    } catch (IOException e) {
    e.printStackTrace();
    }
    t1.start();
    t2.start();
    }
    }class SenderThread extends Thread {
    private PipedOutputStream out = new PipedOutputStream(); PipedOutputStream getPipedOutputStream() {
    return out;
    } public void run() {
    try {
    out.write("www.xmjobs.com".getBytes());
    out.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }class ReceiveThread extends Thread {
    private PipedInputStream in = new PipedInputStream(); PipedInputStream getPipedInputStream() {
    return in;
    } public void run() {
    byte[] buf = new byte[1024];
    int len=0;
    try {
    len = in.read(buf);
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println(new String(buf, 0, len));

    }
    }