1 問題分析
本題中需要一個線程向管道寫入數據,另外一個線程從管道中讀出數據,需要使用到與管道有關的流PipedInputStream和PipedOutputStream。
2 使用管道流
PipedInputStream和PipedOutputStream
管道流用來在線程間進行通信。一個線程的PipedInputStream對象從另一個線程的PipedOutputStream對象讀取輸入。要使管道流有用,必須有一個輸入方和一個輸出方。
PipedInputStream in=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream(in);
3 編寫代碼
[java]
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();
}
}
}
};
}
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
摘自 Java教程