多线程编程-线程间通信.管道(五)
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();
}
}
}
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();
}
}
}
运行结果也是一样的,两个线程可以通过管道进行字符数据的传输。
在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(); } } }
运行结果也是一样的,两个线程可以通过管道进行字符数据的传输。
相关文章推荐
- vc 基于对话框多线程编程实例——线程之间的通信
- 多线程编程之线程间的通信——wait and notify
- Java多线程编程核心技术---线程间通信(一)
- Java 多线程编程之六:线程之间的通信(附源代码)
- PHP多线程编程之管道通信实例分析
- 并发编程线程通信之管道流
- Java多线程编程核心技术---线程间通信(二)
- Java 多线程编程之六:线程之间的通信(附源代码)
- java多线程编程核心技术3-线程间通信
- 【多线程】多线程编程:线程通信
- Java编程之多线程死锁与线程间通信简单实现代码
- 多线程 通过管道进行线程间通信
- PHP多线程编程之管道通信实例分析
- 多线程编程-线程间通信-join方法的使用(六)
- Android多线程编程及线程通信
- Java 多线程编程之六:线程之间的通信(附源代码)
- 【Java编程】多线程之线程间的通信
- 多线程(六)~通过管道进行线程之间的通信:字节流、字符流
- 多线程编程实例----进程通信之命名管道
- 多线程编程之线程间的通信——管道通信