在Java语言中提供了各种各样的输入/输出流Stream,使我们能够很方便地对数据进行操作,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读数据。通过使用管道,实现不同线程间的通信,而无须借助于类似临时文件之类的东西。
在Java的JDK中提供了4个类来使线程间可以进行通信:
1)PipedInputStream和PipedOutputStream
2)PipedReader和PipedWriter
创建测试用的项目pipeInputOutput。
类WriteData.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package service;
import java.io.IOException;
import java.io.PipedOutputStream;
public class{
public void writeMethod(PipedOutputStream out){
try {
System.out.println("write :");
for (int i = 0; i < 300; i++) {
String outData = "" + (i + 1);
out.write(outData.getBytes());
System.out.print(outData);
}
System.out.println();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
类ReadData.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package service;
import java.io.IOException;
import java.io.PipedInputStream;
public class ReadData{
public void readMethod(PipedInputStream input){
try {
System.out.println("read :");
byte[] byteArray = new byte[20];
int readLength = input.read(byteArray);
while (readLength != -1) {
String newData = new String(byteArray, 0, readLength);
System.out.print(newData);
readLength = input.read(byteArray);
}
System.out.println();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
两个自定义线程代码如图3-37所示:
类Run.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package test;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import service.ReadData;
import service.WriteData;
import extthread.ThreadRead;
import extthread.ThreadWrite;
public class Run{
public static void main(String[] args){
try {
WriteData writeData = new WriteData();
ReadData readData = new ReadData();
PipedInputStream inputStream = new PipedInputStream();
PipedOutputStream outputStream = new PipedOutputStream();
outputStream.connect(inputStream);
ThreadRead threadRead = new ThreadRead(readData, inputStream);
threadRead.start();
Thread.sleep(2000);
ThreadWrite threadWrite = new ThreadWrite(writeData, outputStream);
threadWrite.start();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
使用代码inputStream.connect(outputStream)或outputStream.connect(inputStream)的作用使两 大专栏 Java技术之线程通信个Stream之间产生通信链接,这样才可以将数据进行输出与输入。
程序运行结果如图3-38所示:
2. 通过管道进行线程间通信:字符流
当然,在管道中还可以传递字符流。
创建测试用的项目pipeReaderWriter。
类WriteData.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package service;
import java.io.IOException;
import java.io.PipedWriter;
public class{
public void writeMethod(PipedWriter out){
try {
System.out.println("write :");
for (int i = 0; i < 300; i++) {
String outData = "" + (i + 1);
out.write(outData);
System.out.print(outData);
}
System.out.println();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
类ReadData.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package service;
import java.io.IOException;
import java.io.PipedReader;
public class ReadData{
public void readMethod(PipedReader input){
try {
System.out.println("read :");
char[] byteArray = new char[20];
int readLength = input.read(byteArray);
while (readLength != -1) {
String newData = new String(byteArray, 0, readLength);
System.out.print(newData);
readLength = input.read(byteArray);
}
System.out.println();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
两个自定义线程代码如图3-39所示:
类Run.java代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29package test;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import service.ReadData;
import service.WriteData;
import extthread.ThreadRead;
import extthread.ThreadWrite;
public class Run{
public static void main(String[] args){
try {
WriteData writeData = new WriteData();
ReadData readData = new ReadData();
PipedReader inputStream = new PipedReader();
PipedWriter outputStream = new PipedWriter();
outputStream.connect(inputStream);
ThreadRead threadRead = new ThreadRead(readData, inputStream);
threadRead.start();
Thread.sleep(2000);
ThreadWrite threadWrite = new ThreadWrite(writeData, outputStream);
threadWrite.start();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
程序运行结果如图3-40所示:
打印的结果和前一个示例基本一样,此实验是在两个线程中通过管道流进行字符数据的传输。