• Thread--线程间通信--管道


    在Java语言中提供了各种各样的输入/输出流Stream,使我们能够方便的对数据进行操作,其中管道流是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读数据。通过使用管道,实现不同线程间的通信,而无须借助于类似临时文件之类的东西。

    在Java的JDK中提供了4个类来使线程间可以进行通信:

      1)PipedInputStream和PipedOutputStream

      2)PipedReader和PipedWriter

    1)PipedInputStream和PipedOutputStream

     1 package pipeInputOutput;
     2 
     3 import java.io.PipedOutputStream;
     4 
     5 public class WriteData {
     6     
     7     public void writeMethod(PipedOutputStream out) {
     8         try {
     9             System.out.println("write :");
    10             for(int i=0; i<30000; i++) {
    11                 String outData = "" + (i+1);
    12                 out.write(outData.getBytes());
    13                 System.out.print(outData);
    14             }
    15             System.out.println();
    16             out.close();
    17         } catch (Exception e) {
    18             // TODO: handle exception
    19             e.printStackTrace();
    20         }
    21     }
    22 
    23 }
     1 package pipeInputOutput;
     2 
     3 import java.io.PipedInputStream;
     4 
     5 public class ReadData {
     6     
     7     public void readMethod(PipedInputStream in) {
     8         try {
     9             byte[] byteArray = new byte[20];
    10             int readLength = in.read(byteArray);
    11             while(readLength != -1) {
    12                 System.out.print("read :");
    13                 String newData = new String(byteArray, 0, readLength);
    14                 System.out.println(newData);
    15                 readLength = in.read(byteArray);
    16             }
    17             System.out.println();
    18             in.close();
    19         } catch (Exception e) {
    20             // TODO: handle exception
    21             e.printStackTrace();
    22         }
    23     }
    24 
    25 }
     1 package pipeInputOutput;
     2 
     3 import java.io.PipedOutputStream;
     4 
     5 public class ThreadWrite extends Thread {
     6 
     7     private WriteData write;
     8     private PipedOutputStream out;
     9 
    10     public ThreadWrite(WriteData write, PipedOutputStream out) {
    11         super();
    12         this.write = write;
    13         this.out = out;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         write.writeMethod(out);
    19     }
    20     
    21     
    22 
    23 }
     1 package pipeInputOutput;
     2 
     3 import java.io.PipedInputStream;
     4 
     5 public class ThreadRead extends Thread {
     6 
     7     private ReadData read;
     8     private PipedInputStream in;
     9 
    10     public ThreadRead(ReadData read, PipedInputStream in) {
    11         super();
    12         this.read = read;
    13         this.in = in;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         read.readMethod(in);
    19     }
    20 
    21 }
     1 package pipeInputOutput;
     2 
     3 import java.io.PipedInputStream;
     4 import java.io.PipedOutputStream;
     5 
     6 public class Run {
     7     
     8     public static void main(String[] args) {
     9         try {
    10             WriteData writeData = new WriteData();
    11             ReadData readData = new ReadData();
    12             
    13             PipedInputStream inputStream = new PipedInputStream();
    14             PipedOutputStream outputStream = new PipedOutputStream();
    15             
    16             outputStream.connect(inputStream);
    17             
    18 //            outputStream.write("hello world".getBytes());
    19 //            byte[] bs = new byte[100];
    20 //            int i = inputStream.read(bs);
    21 //            System.out.println(new String(bs,0,i));
    22             
    23             ThreadRead threadRead = new ThreadRead(readData, inputStream);
    24             threadRead.start();
    25             
    26             Thread.sleep(2000);
    27             
    28             ThreadWrite threadWrite = new ThreadWrite(writeData, outputStream);
    29             threadWrite.start();
    30         } catch (Exception e) {
    31             // TODO: handle exception
    32             e.printStackTrace();
    33         }
    34     }
    35 
    36 }

    2)PipedReader和PipedWriter

     1 package pipeReaderWriter;
     2 
     3 import java.io.PipedWriter;
     4 
     5 public class WriteData {
     6     
     7     public void writeMethod(PipedWriter out) {
     8         try {
     9             System.out.println("write :");
    10             for(int i=0; i<300; i++) {
    11                 String outData = "" + (i+1);
    12                 out.write(outData);
    13                 System.out.println(outData);
    14             }
    15             System.out.println();
    16             out.close();
    17         } catch (Exception e) {
    18             // TODO: handle exception
    19             e.printStackTrace();
    20         }
    21     }
    22 
    23 }
     1 package pipeReaderWriter;
     2 
     3 import java.io.PipedReader;
     4 
     5 public class ReadData {
     6     
     7     public void readMethod(PipedReader in) {
     8         try {
     9             System.out.println("read:");
    10             char[] byteArray = new char[20];
    11             int readLength = in.read(byteArray);
    12             while(readLength != -1) {
    13                 String newData = new String(byteArray, 0, readLength);
    14                 System.out.println(newData);
    15                 readLength = in.read(byteArray);
    16             }
    17             System.out.println();
    18             in.close();
    19         } catch (Exception e) {
    20             // TODO: handle exception
    21             e.printStackTrace();
    22         }
    23     }
    24 
    25 }
     1 package pipeReaderWriter;
     2 
     3 import java.io.PipedWriter;
     4 
     5 public class ThreadWrite extends Thread {
     6 
     7     private WriteData write;
     8     private PipedWriter out;
     9 
    10     public ThreadWrite(WriteData write, PipedWriter out) {
    11         super();
    12         this.write = write;
    13         this.out = out;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         write.writeMethod(out);
    19     }
    20 
    21 }
     1 package pipeReaderWriter;
     2 
     3 import java.io.PipedReader;
     4 
     5 public class ThreadRead extends Thread {
     6 
     7     private ReadData read;
     8     private PipedReader in;
     9 
    10     public ThreadRead(ReadData read, PipedReader in) {
    11         super();
    12         this.read = read;
    13         this.in = in;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         read.readMethod(in);
    19     }
    20 
    21 }
     1 package pipeReaderWriter;
     2 
     3 import java.io.PipedReader;
     4 import java.io.PipedWriter;
     5 
     6 public class Run {
     7 
     8     public static void main(String[] args) {
     9         try {
    10             WriteData writeData = new WriteData();
    11             ReadData readData = new ReadData();
    12             
    13             PipedWriter out = new PipedWriter();
    14             PipedReader in = new PipedReader();
    15             
    16             in.connect(out);
    17             
    18             ThreadRead threadRead = new ThreadRead(readData, in);
    19             threadRead.start();
    20             
    21             Thread.sleep(2000);
    22             
    23             ThreadWrite threadWrite = new ThreadWrite(writeData, out);
    24             threadWrite.start();
    25             
    26         } catch (Exception e) {
    27             // TODO: handle exception
    28         }
    29     }
    30     
    31 }
  • 相关阅读:
    Hadoop出现 Wrong FS: hdfs://......错误的解决方法
    在Linux下安装JDK环境
    卸载Linux自带的JDK
    hadoop1.2.1伪分布模式安装教程
    spring配置bean的生命周期
    spring注入的四种方式
    python re模块search()与match()区别
    VB.NET操作Excel
    位运算
    Python简单源码解析
  • 原文地址:https://www.cnblogs.com/microcat/p/6370349.html
Copyright © 2020-2023  润新知