首页 > 其他分享 >PipedInputStream

PipedInputStream

时间:2022-08-31 17:11:35浏览次数:77  
标签:receive PipedInputStream printStackTrace PipedOutputStream new out

  也是第一次听说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();
        }
    }

}

 

标签:receive,PipedInputStream,printStackTrace,PipedOutputStream,new,out
From: https://www.cnblogs.com/juniorMa/p/16643758.html

相关文章