多线程使用PipedStream 通讯

Java 提供了四个相关的管道流,我们可以使用其在多线程进行数据传递,其分别是

类名 作用 备注

PipedInputStream	字节管道输入流	字节流
PipedOutputStream 字节管道输出流 字节流
PipedReader 字符管道读取 字符流
PipedWriter 字符管道写入 字符流

其分为两类:字节流和字符流,基本步骤为:线程A写入数据到输出流/写入,线程B读取数据从输入流/字符读取,从而实现线程通讯,下面我们先看下基于字节流的实现方法.

写数据到输出流

package com.zhoutao.demo.thread.piped;

import java.io.IOException;
import java.io.PipedOutputStream;
import java.util.concurrent.TimeUnit; public class WriteData { private int count = 0; public void writeMethod(PipedOutputStream pipedOutputStream) throws InterruptedException, IOException {
while (true) {
// 每隔1s向输出流写入数字字符串
pipedOutputStream.write(String.valueOf(count++).getBytes());
TimeUnit.SECONDS.sleep(1);
}
}
}
读数据从输入流
package com.zhoutao.demo.thread.piped; import java.io.IOException;
import java.io.PipedInputStream; public class ReadData { public void readMethod(PipedInputStream inputStream) throws IOException {
byte[] bytes = new byte[20];
int read;
// 当流中不存在数据时候,read方法会进入阻塞状态
while ((read = inputStream.read(bytes)) != -1) {
String newData = new String(bytes, 0, read);
System.out.println("Get Data = " + newData);
}
}
}

启动测试

package com.zhoutao.demo.thread.piped;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream; public class PipesStreamDemo { public static void main(String[] args) throws IOException {
// 创建读写对象
WriteData writeData = new WriteData();
ReadData readData = new ReadData(); // 创建管道输入输出流
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream(); // 重点:连接管道流
pipedOutputStream.connect(pipedInputStream); // 创建对应的线程并启动
ThreadRead threadRead = new ThreadRead(readData, pipedInputStream);
ThreadWrite threadWrite = new ThreadWrite(writeData, pipedOutputStream);
threadRead.start();
threadWrite.start(); // 观察控制台输出的数据
} static class ThreadRead extends Thread {
private ReadData readData; private PipedInputStream inputStream; public ThreadRead(ReadData readData, PipedInputStream inputStream) {
this.readData = readData;
this.inputStream = inputStream;
} @Override
public void run() {
try {
readData.readMethod(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
} static class ThreadWrite extends Thread {
private WriteData writeData; private PipedOutputStream pipedOutputStream; public ThreadWrite(WriteData writeData, PipedOutputStream pipedOutputStream) {
this.writeData = writeData;
this.pipedOutputStream = pipedOutputStream;
} @Override
public void run() {
try {
writeData.writeMethod(pipedOutputStream);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

测试数据

Get Data = 0
Get Data = 1
Get Data = 2
Get Data = 3
Get Data = 4
Get Data = 5
Get Data = 6
Get Data = 7

java中PipedStream管道流通信详细使用(详解)的更多相关文章

  1. JAVA中的四种JSON解析方式详解

    JAVA中的四种JSON解析方式详解 我们在日常开发中少不了和JSON数据打交道,那么我们来看看JAVA中常用的JSON解析方式. 1.JSON官方 脱离框架使用 2.GSON 3.FastJSON ...

  2. Java中的String,StringBuffer,StringBuilder详解与区别

    1.String Java中string类是不可变的,其中在声明的源代码中用的final,所以只能声明一次.所以每次在明面上的改变其实是重新生成一个String对象,指针指向新的String对象.同时 ...

  3. java中String是对象还是类?详解java中的String

    有很多人搞不懂对象和类的定义.比如说java中String到底是对象还是类呢? 有人说String 既可以说是类,也可以说是对象. 其实他这么说也没问题, 类和对象其实都是一个抽象的概念. 我们可以把 ...

  4. C++/Java中继承关系引发的调用关系详解

    C++: 这里引用到了 http://blog.csdn.net/haoel/article/details/1948051/ 中的内容,还请提前阅读陈大神的这篇博客后在阅读本篇. 覆盖,实现多态的基 ...

  5. Java中的双重检查(Double-Check)详解

    在 Effecitve Java 一书的第 48 条中提到了双重检查模式,并指出这种模式在 Java 中通常并不适用.该模式的结构如下所示: ? 1 2 3 4 5 6 7 8 9 10 public ...

  6. 线程:Java中wait、notify、notifyAll使用详解

    基础知识 首先我们需要知道,这几个都是Object对象的方法.换言之,Java中所有的对象都有这些方法. public final native void notify(); public final ...

  7. Java中的宏变量,宏替换详解。

    群友在微信群讨论的一个话题,有点意思,特拿出来分享一下. 首先来看下面这段程序,和群友分享的大致一样. public static void main(String[] args) { String ...

  8. JAVA中this的三种用法的详解

    this关键字必须放在非静态方法里面 this关键字代表自身,在程序中主要的使用用途有以下几个方面: ? 使用this关键字引用成员变量 ? 使用this关键字在自身构造方法内部引用其它构造方法 ? ...

  9. JAVA中list,set,数组之间的转换详解

    JAVA的list,set,数组之间的转换,主要是使用Apache Jakarta Commons Collections,具体的方法如下:import org.apache.commons.coll ...

随机推荐

  1. 设计模式之GOF23代理模式02

    静态代理 模拟经纪人与明星开演唱会 public interface Star { /**  * 面谈  */  void confer();  /**   * 签合同   */  void sign ...

  2. 1013 Battle Over Cities (25分) 图的连通分量+DFS

    题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...

  3. 热修复框架Tinker快速集成

    由于腾讯官方的demo对于刚接触的我来说,太过复杂,找不到核心配置,因此将tinker集成中最核心的东西抽取出来,整合到一个demo中. demo工程已经提交到github上,点击跳转 更多使用方法, ...

  4. Redux:data flow

    我们使用react,是为了构建可复用的高性能的视图层,学习redux是为了处理视图组件中随应用复杂度提升而变得难以控制的state.说白了,是为了视图. 在了解了action.reducer和stor ...

  5. orcle报错:ORA-12737:Instant Client Light:unsupported server character set ZHS16GBK

    我们用Navacat连接Oracle数据库的时候,会提示ORA-12737:Instant Client Light:unsupported server character set ZHS16GBK ...

  6. HDU3829 Cat VS Dog

    题目链接:https://vjudge.net/problem/HDU-3829 题目大意: 有\(P\)个小孩,\(N\)只猫,\(M\)只狗.每个小孩都有自己喜欢的某一只宠物和讨厌的某一只宠物(其 ...

  7. win-sudo插件解决Git bash 执行脚本报错问题 bash: sudo: command not found

    Windows git bash 默认没有sudo命令,可以添加win-sudo插件实现该功能 curl -s https://raw.githubusercontent.com/imachug/wi ...

  8. zwx_helper 只用小括号()和中括号[ ] 轻松开发wxWidgets

    https://github.com/bbqz007/zhelper-wxWidgets https://github.com/bbqz007/zhelper-wxWidgets/tree/maste ...

  9. 【python基础】datetime类各种坑

    import datetime end_time = 1525104000000 d = datetime.datetime.fromtimestamp(end_time / 1000, None) ...

  10. Docker搭建VS Code Server ,设置访问密码随时随地写代码

    今天在N1盒子上安装了 VS Code Server,简单的记录一下. 安装docker Docker一键安装脚本 $ sudo wget -qO- https://get.docker.com/ | ...