您的位置:首页 > 编程语言 > Java开发

Java_管道流实现

2015-10-21 14:53 507 查看
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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: