JAVAにおけるパイプラインPiped入出力フロー


パイプライン入出力ストリームと通常のファイル入出力ストリームまたはネットワーク入出力ストリームの違いは、主にスレッド間のデータ転送に用いられ、伝送される媒体はメモリであることである。パイプライン入出力ストリームには主に以下の4つの具体的な実装が含まれています。PipedOutputStream、PipedInputStream、PipedReader、PipedWriter、前の2つはバイト向け、後の2つは文字向けです。
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
**           ,     connect()  ,      /       ,             **
/*           
 * 
 * /
 */
class write implements Runnable{
    private PipedOutputStream out = null;
    public void run() {
        String s = "abc";
        try {
            out.write(s.getBytes());
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
    write(PipedOutputStream out){
        this.out = out;
    }
}
class read implements Runnable{
    private PipedInputStream in = null;
    public void run() { 
        byte[] buf = new byte[1024];
        try {
            int len = in.read(buf);
            String s=new String(buf,0,len);
            System.out.println(s);
            in.close();
        }catch (IOException e) {    
            e.printStackTrace();
        }
    }
    read(PipedInputStream in){
        this.in=in; 
    }   
}

public class guandaodemo {
    public static void main(String args[]) throws IOException{


        PipedOutputStream out= new PipedOutputStream(); 
        PipedInputStream in=new PipedInputStream ();
        out.connect(in);//--------------------->>>>>>>>>>        
        write w=new write(out);
        read r=new read(in);

        Thread t1 = new Thread(w);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start(); 
    }
}
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
class writer implements Runnable{
    private PipedWriter out = null;
    public void run() {
        String s = "abc";
        try {
            out.write(s);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }
    writer(PipedWriter out){
        this.out = out;
    }
}
class read implements Runnable{
    private PipedReader in = null;
    public void run() { 
        try {
            int receive = 0;
            while ((receive = in.read()) != -1) {
                System.out.print((char)receive);
            }
            in.close();
        }catch (IOException e) {    
            e.printStackTrace();
        }
    }
    read(PipedReader in){
        this.in = in;   
    }   
}
public class PipedDemo  {
    public static void main(String args[]) throws IOException{
        PipedWriter out = new PipedWriter(); 
        PipedReader in = new PipedReader();
        out.connect(in);//--------------------->>>>>>>>>>        
        writer w = new writer(out);
        read r = new read(in);
        Thread t1 = new Thread(w);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start(); 
    }
}