练习 2015-08-15 管道流 用于线程之间交互数据
来源:互联网 发布:幼儿认字软件 编辑:程序博客网 时间:2024/05/19 08:38
package piped;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class ThreadA implements Runnable {
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOut() {
return out;
}
public void run() {
try {
for (int i = 65; i < 65 + 26; i++) {
out.write(i);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ThreadB implements Runnable{
private PipedInputStream in =null;
ThreadB(ThreadA a) throws Exception{
this.in = new PipedInputStream(a.getOut());
}
public void run() {
int len =-1;
try {
while((len = in.read()) != -1){
System.out.println((char)len);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//管道流
public class PipedDemo {
public static void main(String[] args) throws Exception {
ThreadA a = new ThreadA();
ThreadB b = new ThreadB(a);
new Thread(a).start();
new Thread(b).start();
}
}
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class ThreadA implements Runnable {
private PipedOutputStream out = new PipedOutputStream();
public PipedOutputStream getOut() {
return out;
}
public void run() {
try {
for (int i = 65; i < 65 + 26; i++) {
out.write(i);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ThreadB implements Runnable{
private PipedInputStream in =null;
ThreadB(ThreadA a) throws Exception{
this.in = new PipedInputStream(a.getOut());
}
public void run() {
int len =-1;
try {
while((len = in.read()) != -1){
System.out.println((char)len);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//管道流
public class PipedDemo {
public static void main(String[] args) throws Exception {
ThreadA a = new ThreadA();
ThreadB b = new ThreadB(a);
new Thread(a).start();
new Thread(b).start();
}
}
阅读全文
0 0
- 练习 2015-08-15 管道流 用于线程之间交互数据
- java 管道流的读写操作 用于两个线程之间的通信
- android不同线程之间数据交互
- Exchanger 实现两个伙伴线程之间的数据交互
- 安编程中使用handleMessage实现线程之间数据交互
- 线程之间通过管道通信
- 线程锁用于进程之间
- 例2:使用管道流完成线程之间的通讯。
- 管道流练习
- java 线程之间的管道通信
- homework之线程之间的管道通信
- Activity之间的数据交互
- 双向管道的,两个程序之间的交互
- 线程之间交换数据
- 线程之间共享数据
- 多线程之间交互线程之BlockingQueue
- Java数据交互管道——IO流(输入输出流详解)
- Exchanger用于线程间交换数据
- [cocos2d-x]捕鱼达人炮台射击角度的旋转实现
- ssh基于注解开发 配置文件讲解
- Linux新手需要知道的几个基本指令
- 选数(NOIP2002)
- redis--集群
- 练习 2015-08-15 管道流 用于线程之间交互数据
- hadoop的基本简介及安装、配置、使用(一)
- C++ 类中特殊的成员变量(常变量、引用、静态)的初始化方法
- 四色猜想
- 69. Sqrt(x)
- 2018.8.15总结3-生日聚会
- IDEA集成Git版本控制工具—配置Git和GitHub
- 关于 Android 程序员最近的状况
- Linux驱动学习(-)