java线程间通信管道_通过管道进行线程间通信

管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读数据。通过管道,实现不同线程间的通信,而无须借助类似共享变量、临时文件之类的东西。

在java的JDK中提供了4个类来使线程间可以进行通信:管道字节流 PipedInputStream,PipedOutputStream

管道字符流 PipedReader,PipedWriter

案例

应用场景:1个线程负责写数据,1个线程负责接收数据。

写入线程:class ThreadOut extends Thread {

PipedOutputStream out;

public ThreadOut(PipedOutputStream out) {

this.out = out;

}

@Override

public void run() {

System.out.println("write:");

try {

String message = "hello world! You will be destoryed";

out.write(message.getBytes());

System.out.println("写入数据------" + message);

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

接收线程:class ThreadRead extends Thread {

PipedInputStream input;

public ThreadRead(PipedInputStream input) {

this.input = input;

}

@Override

public void run() {

System.out.println("read:");

try {

byte[] buffer = new byte[10];

int i = -1;

while ((i=(input.read(buffer))) != -1) {

System.out.println("读取数据----"+new String(buffer,0,i));

}

input.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

启动类:public class TestPipe {

public static void main(String[] args) throws IOException {

PipedInputStream inputStream = new PipedInputStream();

PipedOutputStream outputStream = new PipedOutputStream();

inputStream.connect(outputStream);

ThreadOut threadOut = new ThreadOut(outputStream);

ThreadRead threadRead = new ThreadRead(inputStream);

threadOut.start();

threadRead.start();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值