Java 程序设计基础.ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4.4 多线程问题---线程间的通信 1. 线程间的通信可以用管道流,. 创建管道流: PipedInputStream pis=new PipedInputStream(); PipedOutputStream pos=new PipedOutputStream(pis); 或: PipedOutputStream pos=new PipedOutputStream(); PipedInputStream pis=new PipedInputStream(pos); 线程1 PipedOutputStream PipedInputStream 输出流outStream 输入流inStream 线程2 4.4 多线程问题---线程间的通信 1.管道流不能直接读写 PrintStream p = new PrintStream( pos ); p.println(“hello”); DataInputStream d=new DataInputStream(pis); d.readLine(); 2. 通过一个中间类来传递信息. 线程2 线程1 中间类m s s m.write(s) s=m.read() write() read() printStream DataInputStream 4.4 多线程问题--线程间的通信 管道流可以连接两个线程间的通信 下面的例子里有两个线程在运行,一个往外输出信息,一个读入信息. 将一个写线程的输出通过管道流定义为读线程的输入. outStream = new PipedOutputStream(); inStream = new PipedInputStream(outStream); new Writer( outStream ).start(); new Reader( inStream ).start(); (thread\Pipethread.class--f3.bat) 4.4 多线程问题--线程间的通信 主类Pipethread 辅类Writer 线 程 类 辅类 Reader 线 程 类 管 道 流 将数据写 到输出流 从流中读数据 输入流 作为参数传给Writer Writer( outStream ) 4.4 多线程问题--线程间的通信 public class Pipethread { public static void main(String args[]) { Pipethread thisPipe = new Pipethread(); thisPcess(); } public void process() { PipedInputStream inStream; PipedOutputStream outStream; PrintStream printOut; try{ outStream = new PipedOutputStream(); inStream = new PipedInputStream(outStream); new Writer( outStream ).start(); new Reader( inStream ).start(); }catch( IOException e ){ } } } 4.4 多线程问题---线程间的通信 class Reader extends Thread { private PipedInputStream inStream;//从中读数据 public Reader(PipedInputStream i) { inStream = i; } public void run() { String line; DataInputStream d; boolean reading = true; try{ d = new DataInputStream( inStream ); while( reading d != null){ try{line = d.readLine(); if( line != null ){ System.out.println( ”Read: + line );

文档评论(0)

5566www + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

版权声明书
用户编号:6122115144000002

1亿VIP精品文档

相关文档