黑马程序员技术交流社区

标题: 关于PipedInputStream的问题 [打印本页]

作者: 吴亨    时间: 2011-12-16 16:50
标题: 关于PipedInputStream的问题
本帖最后由 吴亨 于 2011-12-17 12:30 编辑

import java.io.*;

class Sender extends Thread
{
        public PipedOutputStream outs;
        public Sender(PipedOutputStream outs)
        {
                this.outs = outs;
        }
        public void run()
{
          try
          {
                outs.write("Hello,Receiver!".getBytes());
                outs.close();
         } catch(Exception ex){}
        }       
       
}

class Receiver extends Thread
{
        public PipedInputStream inp;
        public Receiver(PipedInputStream inp)
        {
                this.inp = inp;
                try
          {
                inp.close();
                } catch(Exception ex){}
        }
        public void run()
        {try
          {
               
                byte[] buf = new byte[1024];
                inp.read(buf);
               
                System.out.println("following: "+new String(buf));
                } catch(Exception ex){}
        }
}

class TestPiped
{
        public static void main(String[]args) throws Exception
        {
                PipedOutputStream outs = new PipedOutputStream();
                Sender sen = new Sender(outs);
                PipedInputStream inp = new PipedInputStream();
                Receiver rec = new Receiver(inp);
                outs.connect(inp);
                sen.start();
                rec.start();
               
        }
}
Sender与Receiver之间没有建立通信吗?

QQ截图20111126164923.png (3.46 KB, 下载次数: 41)

QQ截图20111126164923.png

作者: 吴亨    时间: 2011-12-16 16:57
是我写错了,不好意思。
作者: 刘基军    时间: 2011-12-16 18:28
结贴了?!
作者: 吴亨    时间: 2011-12-17 12:30
monghuan 发表于 2011-12-16 18:28
结贴了?!

monghuan哥,不好意思,这次又是小弟不小心 多写了 inp.close();




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2