java:管道流(线程间管道流)
2023-09-14 08:57:51 时间
class Send implements Runnable{ PipedOutputStream pos = null; public Send() { this.pos = new PipedOutputStream(); } public PipedOutputStream getPipedOutputStream() { return this.pos; } @Override public void run() { // TODO 自动生成的方法存根 String str = "hello world!!!"; try { this.pos.write(str.getBytes()); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } try { this.pos.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } class Receive implements Runnable{ PipedInputStream pis = null; public Receive(){ this.pis = new PipedInputStream(); } public PipedInputStream getPipedInputStream() { return this.pis; } @Override public void run() { // TODO 自动生成的方法存根 byte b[] = new byte[1024]; int len = 0; try { len = this.pis.read(b); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } try { this.pis.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println(new String(b,0,len) ); } } public class PipedOutputStreamDemo { public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 Send send = new Send(); Receive rec = new Receive(); send.getPipedOutputStream().connect(rec.getPipedInputStream()); new Thread(send).start(); new Thread(rec).start(); } }
相关文章
- java———发送网络传真[通俗易懂]
- Java进阶:java开源商城系统源码
- java常量有哪些_JAVA常量介绍「建议收藏」
- Java线程池详解「建议收藏」
- java后端开发需要学什么_从事Java后端开发,要学习哪些知识和技能?[通俗易懂]
- java maven 配置环境变量_maven 环境变量的配置详解
- Java高级用法,写个代理侵入你 ?
- 【说站】java守护线程的执行优先级
- flexmark-java
- 【Java】中的String、StringBuffer和StringBuilder的区别
- java并发编程(1):Java多线程-基本线程类-基础知识复习笔记
- java.nio.channels.Pipe使用例子详解编程语言
- 二叉搜索树的java实现详解编程语言
- Java中的多线程你只要看这一篇就够了详解编程语言
- 开启Java之门:访问MySQL数据库(java访问mysql数据库)
- 在Linux中查看Java线程:一种方法(linux查看java线程)
- 新手进阶:从Java开发到Linux系统架构(java转linux)
- 时间Java中优雅的处理Redis过期时间的方法(redisjava过期)
- 锁Redis Java实现的过期锁保证线程安全性(redisjava过期)
- 数据库的操作Java与MySQL搭配:完美数据库操作(java对mysql)
- Java革命Oracle旗下的程序设计利器(java简介oracle)
- java线程阻塞中断与LockSupport使用介绍
- java中request常用方法小结