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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 肖勇 中级黑马   /  2013-9-25 00:32  /  1054 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

什么时候要用管道流啊!他有什么好处?看老毕的视频时,有点不清楚!请各位大神解释下。。。

评分

参与人数 1技术分 +1 收起 理由
黄兴旺 + 1

查看全部评分

1 个回复

倒序浏览

管道流可以实现两个线程之间,二进制数据的传输。

管道流就像一条管道,一端输入数据,别一端则输出数据。通常要分别用两个不同的线程来控制它们。

使用方法如下:
  1. import java.io.IOException;
  2. import java.io.PipedInputStream;
  3. import java.io.PipedOutputStream;

  4. public class PipedInputStreamTest {

  5.         public static void main(String[] args) {
  6.                 //管道输出流
  7.                 PipedOutputStream out = new PipedOutputStream();
  8.                 //管道输入流
  9.                 PipedInputStream in = null;
  10.                 try {
  11.                         //连接两个管道流。或者调用connect(Piped..);方法也可以
  12.                         in = new PipedInputStream(out);
  13.                         Thread read = new Thread(new Read(in));
  14.                         Thread write = new Thread(new Write(out));
  15.                         //启动线程
  16.                         read.start();
  17.                         write.start();
  18.                 } catch (IOException e) {
  19.                         e.printStackTrace();
  20.                 }
  21.         }
  22. }

  23. class Write implements Runnable {
  24.         PipedOutputStream pos = null;

  25.         public Write(PipedOutputStream pos) {
  26.                 this.pos = pos;
  27.         }

  28.         public void run() {
  29.                 try {
  30.                         System.out.println("程序将在3秒后写入数据,请稍等。。。");
  31.                         Thread.sleep(3000);
  32.                         pos.write("wangzhihong".getBytes());
  33.                         pos.flush();
  34.                 } catch (IOException e) {
  35.                         e.printStackTrace();
  36.                 } catch (InterruptedException e) {
  37.                         e.printStackTrace();
  38.                 } finally {
  39.                         try {
  40.                                 if (pos != null) {
  41.                                         pos.close();
  42.                                 }
  43.                         } catch (IOException e) {
  44.                                 e.printStackTrace();
  45.                         }
  46.                 }
  47.         }
  48. }

  49. class Read implements Runnable {
  50.         PipedInputStream pis = null;

  51.         public Read(PipedInputStream pis) {
  52.                 this.pis = pis;
  53.         }

  54.         public void run() {
  55.                 byte[] buf = new byte[1024];
  56.                 try {
  57.                         pis.read(buf);
  58.                         System.out.println(new String(buf));
  59.                 } catch (IOException e) {
  60.                         e.printStackTrace();
  61.                 } finally {
  62.                         try {
  63.                                 if (pis != null) {
  64.                                         pis.close();
  65.                                 }
  66.                         } catch (IOException e) {
  67.                                 e.printStackTrace();
  68.                         }
  69.                 }
  70.         }
  71. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
黄兴旺 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马