PipedInputStream

  也是第一次听说java里有这么个东西,可以在两个线程间通信

  直接上代码

  

public class PipedInputStreamTest {

    public static void main(String[] args) {

        try (PipedOutputStream out = new PipedOutputStream();
             PipedInputStream in = new PipedInputStream(out)) {
            new Thread(() -> {
                try {
                    out.write("hello kl".getBytes(StandardCharsets.UTF_8));
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }).start();
            int receive;
            while ((receive = in.read()) != -1) {
                System.err.print((char) receive);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

 

posted on 2022-08-31 17:04  MaXianZhe  阅读(85)  评论(0编辑  收藏  举报

导航