线程通信之管道流

原文链接:http://www.bdqn.cn/news/201303/8270.shtml

 

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

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

使用方法如下:

[html]  view plain copy
 
  1. import java.io.IOException;  

  2. import java.io.PipedInputStream;  

  3. import java.io.PipedOutputStream;  

  4.  

  5. public class PipedInputStreamTest {  

  6.  

  7.    public static void main(String[] args) {  

  8.        //管道输出流  

  9.        PipedOutputStream out = new PipedOutputStream();  

  10.        //管道输入流  

  11.        PipedInputStream in = null;  

  12.        try {  

  13.            //连接两个管道流。或者调用connect(Piped..);方法也可以  

  14.            in = new PipedInputStream(out);  

  15.            Thread read = new Thread(new Read(in));  

  16.            Thread write = new Thread(new Write(out));  

  17.            //启动线程  

  18.            read.start();  

  19.            write.start();  

  20.        } catch (IOException e) {  

  21.            e.printStackTrace();  

  22.        }  

  23.    }  

  24. }  

  25.  

  26. class Write implements Runnable {  

  27.    PipedOutputStream pos = null;  

  28.  

  29.    public Write(PipedOutputStream pos) {  

  30.        this.pos = pos;  

  31.    }  

  32.  

  33.    public void run() {  

  34.        try {  

  35.            System.out.println("程序将在3秒后写入数据,请稍等。。。");  

  36.            Thread.sleep(3000);  

  37.            pos.write("wangzhihong".getBytes());  

  38.            pos.flush();  

  39.        } catch (IOException e) {  

  40.            e.printStackTrace();  

  41.        } catch (InterruptedException e) {  

  42.            e.printStackTrace();  

  43.        } finally {  

  44.            try {  

  45.                if (pos != null) {  

  46.                    pos.close();  

  47.                }  

  48.            } catch (IOException e) {  

  49.                e.printStackTrace();  

  50.            }  

  51.        }  

  52.    }  

  53. }  

  54.  

  55. class Read implements Runnable {  

  56.    PipedInputStream pis = null;  

  57.  

  58.    public Read(PipedInputStream pis) {  

  59.        this.pis = pis;  

  60.    }  

  61.  

  62.    public void run() {  

  63.        byte[] buf = new byte[1024];  

  64.        try {  

  65.            pis.read(buf);  

  66.            System.out.println(new String(buf));  

  67.        } catch (IOException e) {  

  68.            e.printStackTrace();  

  69.        } finally {  

  70.            try {  

  71.                if (pis != null) {  

  72.                    pis.close();  

  73.                }  

  74.            } catch (IOException e) {  

  75.                e.printStackTrace();  

  76.            }  

  77.        }  

  78.    }  

  79. }  

  80.  

转载于:https://www.cnblogs.com/zhongshiqiang/p/5976206.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值