A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© hdsjsql 中级黑马   /  2013-5-19 18:16  /  1421 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 hdsjsql 于 2013-5-19 18:18 编辑
  1. import java.io.*;

  2. class Read implements Runnable
  3. {
  4.         private PipedInputStream in;
  5.         Read(PipedInputStream in)
  6.         {
  7.                 this.in = in;
  8.         }
  9.         public void run()
  10.         {
  11.                 try
  12.                 {
  13.                         byte[] buf =new byte[1024];
  14.                         

  15.                         System.out.println("读取前。。没有数据阻塞");
  16.                         int len = in.read(buf);
  17.                         System.out.println("读到数据。。阻塞结束");



  18.                         String s= new String(buf,0,len);

  19.                         System.out.println(s);

  20.                         in.close();

  21.                 }
  22.                 catch (IOException e)
  23.                 {
  24.                         throw new RuntimeException("管道读取流失败");
  25.                 }
  26.         }
  27. }

  28. class Write implements Runnable
  29. {
  30.         private PipedOutputStream out;
  31.         Write(PipedOutputStream out)
  32.         {
  33.                 this.out = out;
  34.         }
  35.         public void run()
  36.         {
  37.                 try
  38.                 {
  39.                         System.out.println("开始写入数据,等待6秒后。");
  40.                         Thread.sleep(6000);
  41.                         out.write("piped lai la".getBytes());
  42.                         out.close();
  43.                 }
  44.                 catch (Exception e)
  45.                 {
  46.                         throw new RuntimeException("管道输出流失败");
  47.                 }
  48.         }
  49. }

  50. public class  PipedStreamDemo
  51. {
  52.         public static void main(String[] args) throws IOException
  53.         {

  54.                 PipedInputStream in = new PipedInputStream();
  55.                 PipedOutputStream out = new PipedOutputStream();
  56.                 in.connect(out);                                                      //此处可改为out.connect(in),执行结果相同
  57.                                                                                               //这是说明管道流中的数据时由PipedOutputStream对象输入,
  58.                                                                                               //PipedInputStream对象输出。而且只有这一中传输方向?
  59.                 Read r = new Read(in);
  60.                 Write w = new Write(out);
  61.                 new Thread(r).start();
  62.                 new Thread(w).start();


  63.         }
  64. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘胜寒 + 1

查看全部评分

2 个回复

倒序浏览
看完了代码,再往下一看,吓我一跳,问题呢?
如果?前是问题的话,,,,那么也就是结果,,,你自己都已经总结出来了。。。。。。。{:soso_e127:}
回复 使用道具 举报
out.connect(in)与in.connect(out)效果一样,都是连接管道,无所谓方向问题,因为输入流是阻塞线程,没有输入数据时,它会等待,宏观上来说它是最后执行的。这个程序里,输出流写了一些数据到管道,所以它也输入了,输入流到管道读到取数据显示到控制台,所以它也输出了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马