스 레 드 간 흐름 통신 (파이프라인 흐름)

1489 단어 자바
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.Reader;
import java.io.Writer;
class Send implements Runnable{
	private PipedOutputStream out=null;
	
	public Send(){
		out=new PipedOutputStream();
	}
	@Override
	public void run() {
		String str="Hello World!";
		try {
			out.write(str.getBytes());
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public PipedOutputStream getOut(){
		return this.out;
	}
	
}
class Recive implements Runnable{
	private PipedInputStream in=null;
	public Recive(){
		in=new PipedInputStream();
	}
	@Override
	public void run() {
		byte[] b=new byte[1024];
		try {
			int len=in.read(b);
			in.close();
			System.out.println(new String(b,0,len));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public PipedInputStream getIn(){
		return in;
	}
}
public class Test {  
	  
    public static void main(String[] args) throws IOException {
    	Send send=new Send();
    	Recive recive=new Recive();
    	send.getOut().connect(recive.getIn());
    	new Thread(send).start();
    	new Thread(recive).start();
    }
    
    /**
     *   
		Hello World!
     */
}

좋은 웹페이지 즐겨찾기