• 线程通信-基于字节流管道


    通过管道进行线程通信-字节流:

     1 import java.io.IOException;
     2 import java.io.PipedInputStream;
     3 
     4 /**
     5  *    PipedInputStream
     6  */
     7 public class ReadData {
     8     
     9     public void readMethod(PipedInputStream in) {
    10         try {
    11             System.out.println("read:");
    12             byte[] byteArray = new byte[20];
    13             
    14             int readLength = in.read(byteArray);//读取字节数组大小的数据到字节数组中
    15             
    16             while (readLength != -1) {
    17                 String newData = new String(byteArray, 0, readLength);
    18                 System.out.print(newData);
    19                 readLength = in.read(byteArray);
    20             }
    21             System.out.println();
    22             in.close();
    23         } catch (IOException e) {
    24             e.printStackTrace();
    25         }
    26     }
    27 }
     1 import java.io.IOException;
     2 import java.io.PipedOutputStream;
     3 
     4 /**
     5  *    PipedOutputStream
     6  */
     7 public class WriteData {
     8     
     9     public void writeMethod(PipedOutputStream out) {
    10         try {
    11             System.out.println("write:");
    12             for (int i = 0; i < 300; i++) {
    13                 String outData = "" + (i+1);
    14                 out.write(outData.getBytes());
    15                 System.out.print(outData);
    16             }
    17             System.out.println();
    18             out.close();
    19         } catch (IOException e) {
    20             e.printStackTrace();
    21         }
    22     }
    23 }
     1 import java.io.PipedInputStream;
     2 
     3 /**
     4  *    消费线程
     5  */
     6 public class ThreadRead extends Thread {
     7     
     8     private ReadData read;
     9     private PipedInputStream in;
    10     
    11     public ThreadRead(ReadData read,PipedInputStream in) {
    12         this.read = read;
    13         this.in = in;
    14     }
    15     
    16     @Override
    17     public void run() {
    18         read.readMethod(in);
    19     }
    20 }
     1 import java.io.PipedOutputStream;
     2 
     3 /**
     4  *    生产线程
     5  */
     6 public class ThreadWrite extends Thread {
     7     
     8     private WriteData write;
     9     private PipedOutputStream out;
    10     
    11     public ThreadWrite(WriteData write,PipedOutputStream out) {
    12         this.write = write;
    13         this.out = out;
    14     }
    15     
    16     @Override
    17     public void run() {
    18         write.writeMethod(out);
    19     }
    20 }
     1 import java.io.IOException;
     2 import java.io.PipedInputStream;
     3 import java.io.PipedOutputStream;
     4 
     5 /**
     6  *    线程通信测试类
     7  */
     8 public class Run {
     9 
    10     public static void main(String[] args) {
    11         try {
    12             WriteData write = new WriteData();
    13             ReadData read = new ReadData();
    14 
    15             PipedInputStream in = new PipedInputStream();
    16             PipedOutputStream out = new PipedOutputStream();
    17 
    18             out.connect(in);
    19             
    20             ThreadRead threadRead = new ThreadRead(read,in);
    21             threadRead.start();
    22             
    23             Thread.sleep(2000);
    24             
    25             ThreadWrite threadWrite = new ThreadWrite(write,out);
    26             threadWrite.start();
    27         } catch (IOException e) {
    28             e.printStackTrace();
    29         } catch (InterruptedException e) {
    30             e.printStackTrace();
    31         }
    32     }
    33 }

    运行结果如下:

      

    可以看到消费线程启动后,目前没有数据,等待两秒后,生产线程提供数据,即write:下面的第一行数字,此时消费者线程获取到数据,打印最下面一行的数字。

  • 相关阅读:
    Navicat for Mysql安装及破解教程
    如何down掉IB交换机口
    pycharm替换文件中所有相同字段方法
    NAS、SAN、ISCSI存储
    Linux系统下安装rz/sz命令及使用说明
    python 实现查找某个字符在字符串中出现次数,并以字典形式输出
    python class用法
    zookeeper
    机器学习基础
    hive--数据仓库
  • 原文地址:https://www.cnblogs.com/wang1001/p/9566547.html
Copyright © 2020-2023  润新知