您的位置:首页 > 编程语言

多线程编程-线程间通信.管道(五)

2018-01-17 14:58 525 查看
通过管道进行线程间通信:字节流(PipedOutputStream,PipedInputStream)
在Java中提供了各种各样的输入/输出流Stream,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读数据。使用管道实现线程间的通信,不需要借助类似临时文件之类的东西。
测试代码:
public class WriteData {
public void writeMethod(PipedOutputStream pos) {
try {
System.out.println("write :");
for (int i = 0; i < 300; i++) {
String outData = "" + (i + 1);
pos.write(outData.getBytes());
System.out.print(outData);
}
System.out.println();
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ReadData {
public void readMethod(PipedInputStream pis){
try{
System.out.println("Read :");
byte[] byteArray = new byte[20];
int readLength = pis.read(byteArray);
while(readLength != -1){
String newData = new String(byteArray, 0, readLength);
System.out.print(newData);
readLength = pis.read(byteArray);
}
System.out.println();
pis.close();
}catch(IOException e){

}
}
}
public class ThreadWrite extends Thread{
private WriteData wd;
private PipedOutputStream pos;
public ThreadWrite(WriteData wd, PipedOutputStream pos) {
super();
this.wd = wd;
this.pos = pos;
}

@Override
public void run(){
wd.writeMethod(pos);
}
}
public class ThreadRead extends Thread{
private ReadData rd;
private PipedInputStream pis;
public ThreadRead(ReadData rd, PipedInputStream pis) {
this.rd = rd;
this.pis = pis;
}

@Override
public void run(){
rd.readMethod(pis);
}
}
public class RunTest {

public static void main(String[] args) {
try{
WriteData wd = new WriteData();
ReadData rd = new ReadData();
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pos.connect(pis);
//pis.connect(pos);
ThreadRead tr = new ThreadRead(rd, pis);
tr.start();
Thread.sleep(2000);
ThreadWrite tw = new ThreadWrite(wd, pos);
tw.start();
}catch(IOException | InterruptedException e){
e.printStackTrace();
}
}
}
运行结果:
Read :

write :

123456789101112123456789101....

读取线程启动后,由于还没数据写入,所以线程阻塞在read中,直到有数据写入,继续运行。

通过管道进行进程间通信:字符流(PipedWriter,PipedReader)
在管道中也可以传递字符流。
测试代码:
public class ReadData {
public void readMethod(PipedReader pis){
try{
System.out.println("Read :");
char[] byteArray = new char[20];
int readLength = pis.read(byteArray);
while(readLength != -1){
String newData = new String(byteArray, 0, readLength);
System.out.print(newData);
readLength = pis.read(byteArray);
}
System.out.println();
pis.close();
}catch(IOException e){

}
}
}
public class WriteData {
public void writeMethod(PipedWriter pos) {
try {
System.out.println("write :");
for (int i = 0; i < 300; i++) {
String outData = "" + (i + 1);
pos.write(outData);
System.out.print(outData);
}
System.out.println();
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ThreadWrite extends Thread{
private WriteData wd;
private PipedWriter pos;
public ThreadWrite(WriteData wd, PipedWriter pos) {
super();
this.wd = wd;
this.pos = pos;
}

@Override
public void run(){
wd.writeMethod(pos);
}
}
public class ThreadRead extends Thread{
private ReadData rd;
private PipedReader pis;
public ThreadRead(ReadData rd, PipedReader pis) {
this.rd = rd;
this.pis = pis;
}

@Override
public void run(){
rd.readMethod(pis);
}
}
public class RunTest {

public static void main(String[] args) {
try{
WriteData wd = new WriteData();
ReadData rd = new ReadData();
PipedReader pis = new PipedReader();
PipedWriter pos = new PipedWriter();
pos.connect(pis);
//pis.connect(pos);
ThreadRead tr = new ThreadRead(rd, pis);
tr.start();
Thread.sleep(2000);
ThreadWrite tw = new ThreadWrite(wd, pos);
tw.start();
}catch(IOException | InterruptedException e){
e.printStackTrace();
}
}
}

运行结果也是一样的,两个线程可以通过管道进行字符数据的传输。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: