本章目标
掌握线程通讯流(管道流)的使用
管道流
管道流的主要作用是可以进行两个线程间的通讯,分为管道输出流(PipedOutputStream)、管道输入流(PipedInputStream),如果要想进行管道输出,则必须把输出流连在输入流之上,在PipedOutputStream类上有如下的一个方法用于连接管道:
——public void connect(PipedInputStream snk) throws IOException
管道输出流的线程类
管道输入流的线程类
连接管道的输入和输出
import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; class Send implements Runnable{//实现 Runnable 接口 private PipedOutputStream pos = null;//管道输出流 public Send(){ this.pos = new PipedOutputStream();//实例化输出流 } public void run(){ String str = "Hello World !!!"; try{ this.pos.write(str.getBytes());//输出信息 }catch(IOException e){ e.printStackTrace(); } try{ this.pos.close();//关闭输出流 }catch(IOException e){ e.printStackTrace(); } } public PipedOutputStream getPos(){//通过线程类得到输出流 return pos; } } class Receive implements Runnable{//实现 Runnable 接口 private PipedInputStream pis = null; public Receive(){ this.pis = new PipedInputStream();//实例化输入流 } public void run(){ byte b[] = new byte[1024]; int len = 0; try{ len = this.pis.read(b);//接收数据 }catch(IOException e){ e.printStackTrace(); } try{ this.pis.close(); }catch(IOException e){ e.printStackTrace(); } System.out.println("接收的内容为:"+new String(b,0,len)); } public PipedInputStream getPis(){ return pis; } } public class PipedDemo{ public static void main(String[] args){ Send s = new Send(); Receive r = new Receive(); try{ s.getPos().connect(r.getPis());//连接管道 }catch(IOException e){ e.printStackTrace(); } new Thread(s).start();//启动线程 new Thread(r).start();//启动线程 } /* 结果: * 接收的内容为:Hello World !!! * */ }
相关推荐
SIEMENS WINCC管道流动控件。
用qml做的简易管道流动效果,可以用在工业组态。做的比较简单,如果有好的思路请大家交流。可设定管道颜色,管壁颜色。
针对java中的管道流的应用的解析,包括PipedInputStream和PipedOutputStream。
一个详细讲解JAVA_管道流的例子 希望可以帮助到你。
CHorPipeFlowCtrl实现水平方向流动 CVerPipeFlowCtrl实现垂直方向流动 CLeftTopPipeFlowCtrl实现左边交叉流动 CRightTopPipeFlowCtrl实现右边交叉流动 流动效果基本均匀,可自行设定滑块形状(圆形,矩形,三解,...
WinCC管道流动控件。
基于matlab语言开发,计算管道的瞬变流现象,考虑了温度变化
java管道流 Listing 1:用管道流截取控制台输出】 PipedInputStream pipedIS = new PipedInputStream(); PipedOutputStream pipedOS = new PipedOutputStream(); try { pipedOS.connect(pipedIS); } catch...
OpenFOAM的二维低速层流管道流动算例(泊肃叶流动)-case文件,下载解压后,./Allrun运行。
关于管道流动及充填问题中,为求解时间、压力等的常微分方程组的求解。
以用于矿井主排水管道射流清洗的圆柱形喷嘴为研究对象,利用FLUENT软件对喷嘴内外部流场进行数值模拟,通过改变不同射流压力、不同冲击角、不同环境介质和靶距来分析喷嘴射流的流场特性,得到了流场的射流速度、动压力...
这是一个比较基础的管道流例子和说明,解析得比较详细,需要的朋友可以看看
磁场作用下铁磁流体管道流中的自旋效应,徐双艳,李明军,本文通过分析Yamaguchi讨论的在磁场作用下一类特殊的铁磁流体管道流控制方程,给出了磁场作用下的铁磁流体管道流中产生的磁粘度的表
管道流在已知管壁温度下求解的温度场
unity 管道流动着色器
55-IO流(管道流).avi
JAVA管道流.pdf
Java管道流.pdf
Stream管道流Map操作.pdf
重难点之Java管道流.pdf