フローは、スレッド間の通信のための導管

たInputStreamから継承されたパイプラインの流れ(持つPipedInputStream、PipedOutputStreamの)、スレッド間通信、一般ユーザが、ほとんどはそれを使用しないでください。


テストケース

送信者スレッドの作成

class SendThread implements Runnable{
    private PipedOutputStream outputStream = new PipedOutputStream();
    @Override
    public void run() {
        try {
            outputStream.write("管道对接成功!!".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public PipedOutputStream getOutputStream() {
        return outputStream;
    }
    public void setOutputStream(PipedOutputStream outputStream) {
        this.outputStream = outputStream;
    }
}

受信者のスレッドの作成

class RecieveThread implements Runnable{
    private PipedInputStream inputStream = new PipedInputStream();
    @Override
    public void run() {
        byte[] data = new byte[1024];
        try {
            int len = inputStream.read(data);
            System.out.println("对接情况:"+new String(data,0,len));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public PipedInputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(PipedInputStream inputStream) {
        this.inputStream = inputStream;
    }
}

テスト

public class GuandaoTest {
    public static void main(String[] args) {
        SendThread sendThread = new SendThread();
        RecieveThread recieveThread = new RecieveThread();
        PipedOutputStream outputStream = sendThread.getOutputStream();
        try {
            outputStream.connect(recieveThread.getInputStream());
            new Thread(sendThread).start();
            new Thread(recieveThread).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

分析

パイプフローで受信者に入った、送信者がダクトストリームを出力し、クライアントは、出力ストリーム、そのバットへの入力ストリームを取得し、パイプに相当するが、出力ストリームに、入力ストリームの受信した情報になっています。

公開された47元の記事 ウォンの賞賛6 ビュー2188

おすすめ

転載: blog.csdn.net/weixin_44467251/article/details/103091919