Java_管道流实现

来源:互联网 发布:勇往直前歌词网络歌手 编辑:程序博客网 时间:2024/06/05 19:03
import java.io.*;import java.util.*;import java.lang.*;class Read implements Runnable{private PipedInputStream in;Read(PipedInputStream in){this.in = in;}public void run(){try{byte [] b = new byte[1024];System.out.println("读取前没有数据阻塞!");int len = in.read(b);System.out.println("读到数据阻塞关闭!");String s = new String(b,0,len);System.out.println(s);}catch(IOException e){throw new RuntimeException("读取管道流失败!");}}}class Write implements Runnable{private PipedOutputStream out;Write(PipedOutputStream out){this.out = out;}public void run(){try{System.out.println("请等待6秒后写入!");Thread.sleep(6000);out.write("guan dao liu lai l".getBytes());out.close();}catch(Exception e){throw new RuntimeException("写入管道流失败!");}}}public class PipedText1 {public static void main(String[] args) throws IOException {PipedInputStream in = new PipedInputStream();PipedOutputStream out = new PipedOutputStream();in.connect(out);Read r = new Read(in);Write w = new Write(out);Thread t1 = new Thread(r);Thread t2 = new Thread(w);t1.start();t2.start();}}

0 0
原创粉丝点击