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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王浩龙 中级黑马   /  2014-3-18 11:45  /  721 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.IOException;
  2. import java.io.PipedInputStream;
  3. import java.io.PipedOutputStream;

  4. class Read implements Runnable{
  5.         private PipedInputStream in;
  6.         Read(PipedInputStream in){
  7.                 this.in = in;
  8.         }
  9.         @Override
  10.         public void run() {
  11.                 byte[] b = new byte[1024];
  12.                 int len = 0;
  13.                 try {
  14.                         System.out.println("xiuxiyixia 6miao");
  15.                         Thread.sleep(6000);
  16.                         len = in.read(b);
  17.                         System.out.println(new String(b, 0, len));
  18.                 } catch (Exception e) {
  19.                         throw new RuntimeException("管道流读取失败");
  20.                 }
  21.                 finally{
  22.                                 if(in!=null)
  23.                                 try {
  24.                                         in.close();
  25.                                 } catch (IOException e) {
  26.                                         e.printStackTrace();
  27.                                 }
  28.                 }       
  29.         }
  30.        
  31. }
  32. class Write implements Runnable{
  33.         private PipedOutputStream out;
  34.         Write(PipedOutputStream out){
  35.                 this.out = out;
  36.         }
  37.         @Override
  38.         public void run() {
  39.                 try {
  40.                  out.write("asdasdasfwq".getBytes());;
  41.                 } catch (IOException e) {
  42.                         throw new RuntimeException("管道流写入取失败");
  43.                 }
  44.                 finally{
  45.                                 if(out!=null)
  46.                                 try {
  47.                                         out.close();
  48.                                 } catch (IOException e) {
  49.                                         e.printStackTrace();
  50.                                 }
  51.                 }
  52.                
  53.         }
  54.        
  55. }
  56. public class PipedStreamTest {

  57.         public static void main(String[] args) throws IOException {
  58.                 PipedInputStream in = new PipedInputStream();
  59.                 PipedOutputStream out = new PipedOutputStream();
  60.                 in.connect(out);//这里的我们可以用out去连接in吗?
  61.                 Read r = new Read(in);
  62.                 Write w = new Write(out);
  63.                 new Thread(r).start();
  64.                 new Thread(w).start();       
  65.         }
  66. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
SyouRai_Tsk + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马