zoukankan      html  css  js  c++  java
  • Java技术之线程通信

    在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
    19
    package 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
    21
    package 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
    29
    package 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
    19
    package 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
    21
    package 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
    29
    package 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所示:

    打印的结果和前一个示例基本一样,此实验是在两个线程中通过管道流进行字符数据的传输。

  • 相关阅读:
    欧几里德算法实现求两个正整数的最大公因子
    C#委托、泛型
    C与C++中的time相关函数(转载)
    【转】温州的南拳
    前端面试题
    vuecli卸载旧版,再重新安装后还显示的是旧的版本
    不定宽高的div水平、垂直居中问题
    解决JS中取URL地址中的参数中文乱码
    移动vue项目,启动错误:Module build failed: Error: No PostCSS Config found in:
    codeblocks colour theme
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12433278.html
Copyright © 2011-2022 走看看