PipedInputStream/PipedOutputStream

PipedInputStream和PipedOutputStream分别是管道输入流和管道输出流.

它们的作用是让多线程之间可以通过管道进行通讯,在使用管道通信时,必须将PipedInputStream和PipedOutputStream配合使用.

使用管道通信时,大致流程是:线程A向PipedOutputStream中写入数据,这些数据会自动的发送到对应的pipedInputStream中进行缓存,此时,线程B通过读取PipedInputStream中的数据,就可以实现线程通信了.

实验一:发送简短的消息

Sender.java(发送消息)

public class Sender extends Thread {
    private PipedOutputStream pos = new PipedOutputStream();

    public Sender(PipedOutputStream pos) {
    this.pos = pos;
    }

    @Override
    public void run() {
         sendShortMessage();
    }

    // 发送简单的消息
    public void sendShortMessage() {
    try {
        pos.write("你好啊!".getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
        pos.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
    }
    }
}

Reciver.java(接受消息)

public class Reciver extends Thread {
    private PipedInputStream pis = new PipedInputStream();

    public Reciver(PipedInputStream pis) {
    this.pis = pis;
    }

    @Override
    public void run() {
    byte[] buf = new byte[2048];
    try {
        pis.read(buf);
        pis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Reciver :" + new String(buf));
    }
}

PipedTest.java

public class PipedTest {
    public static void main(String[] args) throws IOException {
    PipedOutputStream pipedOutputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream();
    Sender sender = new Sender(pipedOutputStream);
    Reciver reciver = new Reciver(pipedInputStream);
    pipedInputStream.connect(pipedOutputStream);
    sender.start();
    reciver.start();
    }
}
//Reciver :你好啊!

实验一很好理解:

pipedInputStream.connect(pipedOutputStream);方法把PipedOutPutStream和PipedInputStream关联了起来.
Sender通过write()方法,向PipedInputStream中写入"你好啊!",查看PipedOutputStream的writer()方法可知,其实是调用了PipedinputStream的receive()方法,继续查看该方法可知,PipedinputStream的
receive()方法,其实就是把数据保存到自己的byte buffer[]数组中,而且其数组的默认大小为1024.
Reciver通过read()方法,从自己的byte buffer[]数组中读取数据.

实验二:发送较长的消息

 Sender.java(发送消息)

public class Sender extends Thread {
    private PipedOutputStream pos = new PipedOutputStream();

    public Sender(PipedOutputStream pos) {
    this.pos = pos;
    }

    @Override
    public void run() {
    sendShortMessage();
    }

    // 发送较长的消息
    public void sendLongMessage() {
    StringBuilder sb = new StringBuilder();
    // 总共长度是1020个字节
    for (int i = 0; i < 102; i++) {
        sb.append("0123456789");
    }
    // 再写入26个字节。
    sb.append("abcdefghijklmnopqrstuvwxyz");
    // str的总长度是1020+26=1046个字节
    String str = sb.toString();
    try {
        pos.write(str.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
        pos.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
    }
    }
}

Reciver.java(接受消息)

 

public class Reciver extends Thread {
    private PipedInputStream pis = new PipedInputStream();

    public Reciver(PipedInputStream pis) {
    this.pis = pis;
    }

    @Override
    public void run() {
    byte[] buf = new byte[2048];
    try {
        pis.read(buf);
        pis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Reciver :" + new String(buf));
    }
}

 PipedTest.java

public class PipedTest {
    public static void main(String[] args) throws IOException {
    PipedOutputStream pipedOutputStream = new PipedOutputStream();
    PipedInputStream pipedInputStream = new PipedInputStream();
    Sender sender = new Sender(pipedOutputStream);
    Reciver reciver = new Reciver(pipedInputStream);
    pipedInputStream.connect(pipedOutputStream);
    sender.start();
    reciver.start();
    }
}

 实验二的运行结果为:Reciver :012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789abcd

发现收到的数据少了"efghijklmnopqrstuvwxyz",为什么打印出来的数据正好是1024长度.其实上面我们已经说过了,PipedInputStream的byte [] buffer数组的默认长度只有1024,所以上面的输出是这样的.那怎么改才能输出全部内容的?

我们修改Reciver.java

public class Reciver extends Thread {
    private PipedInputStream pis = new PipedInputStream();

    public Reciver(PipedInputStream pis) {
    this.pis = pis;
    }

    @Override
    public void run() {
    reciverLongMessage();
    }

    public void reciverLongMessage() {
    while (true) {
        byte[] buf = new byte[1024];
        int len;
        try {
        len = pis.read(buf);
        if (len > 0) {
            System.out.println(new String(buf, 0, len));
        } else {
            break;
        }
        } catch (IOException e) {
        try {
            pis.close();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        }
    }
    }
}

 这样就可以正确输出结果啦!

转载于:https://www.cnblogs.com/zhangj-ymm/p/9860674.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用PipedInputStreamPipedOutputStream的简单示例: ```java import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedExample { public static void main(String[] args) { try { // 创建PipedInputStreamPipedOutputStream对象 PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); // 将输入流和输出流连接起来 pipedInputStream.connect(pipedOutputStream); // 创建一个线程用于写入数据到PipedOutputStream Thread writerThread = new Thread(() -> { try { String message = "Hello, World!"; pipedOutputStream.write(message.getBytes()); pipedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }); // 创建一个线程用于从PipedInputStream读取数据 Thread readerThread = new Thread(() -> { try { byte[] buffer = new byte[1024]; int bytesRead = pipedInputStream.read(buffer); while (bytesRead != -1) { System.out.println(new String(buffer, 0, bytesRead)); bytesRead = pipedInputStream.read(buffer); } pipedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } }); // 启动线程 writerThread.start(); readerThread.start(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述示例中,我们首先创建了一个PipedInputStream对象和一个PipedOutputStream对象,并通过调用`connect()`方法将它们连接起来。然后,我们创建一个写线程,使用`write()`方法将字符串数据写入PipedOutputStream中,并在写入完成后关闭流。接下来,我们创建一个读线程,使用`read()`方法从PipedInputStream中读取数据,并在读取完成后关闭流。最后,我们启动这两个线程,观察输出结果。 当我们运行这个示例时,你会看到控制台输出了`Hello, World!`这个字符串。这个字符串是通过PipedOutputStream写入的,并通过PipedInputStream读取的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值