例2:使用管道流完成线程之间的通讯。
2012-03-12 11:13
169 查看
1 问题分析
本题中需要一个线程向管道写入数据,另外一个线程从管道中读出数据,需要使用到与管道有关的流PipedInputStream和PipedOutputStream。
2 使用管道流
PipedInputStream和PipedOutputStream
管道流用来在线程间进行通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须有一个输入方和一个输出方。
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream(in);
3 编写代码
编译运行
javac TestPipeStream.java
java TestPipeStream
本题中需要一个线程向管道写入数据,另外一个线程从管道中读出数据,需要使用到与管道有关的流PipedInputStream和PipedOutputStream。
2 使用管道流
PipedInputStream和PipedOutputStream
管道流用来在线程间进行通信。一个线程的PipedInputStream对象从另一个线程的PipedOutputStream对象读取输入。要使管道流有用,必须有一个输入方和一个输出方。
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream(in);
3 编写代码
import java.io.*; public class TestPipeStream { public static void main(String[] args) { try { PipedInputStream in1 = new PipedInputStream(); PipedOutputStream out1 = new PipedOutputStream(in1); PipedInputStream in2 = new PipedInputStream(); PipedOutputStream out2 = new PipedOutputStream(in2); ThreadC tc = new ThreadC(out1); ThreadZ tz = new ThreadZ(out2, in1); ThreadQ tq = new ThreadQ(in2); tc.start(); tz.start(); tq.start(); } catch (Exception e) { e.printStackTrace(); } } static class ThreadC extends Thread { DataOutputStream dos = null; public ThreadC(OutputStream os) { dos = new DataOutputStream(os); } public void run() { while (true) { try { double d = Math.random(); dos.writeDouble(d); sleep(2); } catch (Exception e) { e.printStackTrace(); } } } }; static class ThreadZ extends Thread { DataOutputStream dos = null; DataInputStream dis = null; public ThreadZ(OutputStream os, InputStream is) { dos = new DataOutputStream(os); dis = new DataInputStream(is); } public void run() { while (true) { try { double d = dis.readDouble(); dos.writeDouble(d); } catch (Exception e) { e.printStackTrace(); } } } }; static class ThreadQ extends Thread { DataInputStream dis = null; public ThreadQ(InputStream is) { dis = new DataInputStream(is); } public void run() { while (true) { try { double d = dis.readDouble(); System.out.println(d); } catch (Exception e) { e.printStackTrace(); } } } }; }
编译运行
javac TestPipeStream.java
java TestPipeStream
相关文章推荐
- 使用管道流完成线程间的通信
- 使用管道流和多线程完成以下任务要求: 一个线程对象t1 会从控制台中不停的读取信息,读到之后把数据交给管道输出流,把数据写到管道里面 ,另一个线程对象t2 不停的使用管道输入流从管道
- [多线程] ManualResetEvent(线程之间通讯 通知或者查看是否完成)
- 线程之间的通讯- UI 通讯使用SynchronizationContext--(1)
- 线程之间的通讯- UI 通讯使用SynchronizationContext--(2)
- 使用泛型SwingWorker与EDT事件分发线程保持通讯
- 主线程与子线程之间通讯, 子线程与子线程之间的通讯
- java中使用ReentrantLock锁中的Condition实现三个线程之间通信,交替输出信息
- .NET与JSP页面之间消息通讯(跨域,使用html5的postMessage实现)
- Java--线程同步(使用了synchronized)和线程通讯(使用了wait,notify)
- 多线程篇(二)——线程之间的同步与互斥,关键段的使用
- java线程基础巩固---多Produce多Consume之间的通讯导致出现程序假死的原因分析
- 使用mob的即时通讯无法完成注册
- java线程之间的调度使用wait/notify,await/single,LinkBlockingQuene实现
- 分别使用面向过程的方法和面向对象的方法,完成如下命题:求 两点之间的距离。
- GUI(主)线程与子线程之间的通信(用信号槽通讯)
- C#学习之UI线程和线程池线程之间通讯之TaskScheduler.FromCurrentSynchronizationContext()
- SpringMVC使用注解完成 URL 和 Controller & 方法之间的映射细节
- 使用Handler来对线程之间传值
- EventBus的使用详解,功能为在Fragment,Activity,Service,线程之间传递消息