package 多线程流; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedInputStreamDemo1 { public static void main(String[] args) throws IOException { PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); InputStream inputStream = new InputStream(pipedInputStream); OutputStream outputStream = new OutputStream(pipedOutputStream); pipedInputStream.connect(pipedOutputStream); Thread thread = new Thread(inputStream); Thread thread1 = new Thread(outputStream); thread.start(); thread1.start(); } } class InputStream implements Runnable { private final PipedInputStream p; public InputStream(PipedInputStream p) { this.p = p; } @Override public void run() { try { // int read = p.read(); byte[] bytes = new byte[1024]; // int len = 0; int len = p.read(bytes); String Buffer = new String(bytes,0,len); System.out.println(Buffer); } catch (IOException e) { e.printStackTrace(); } finally { try { p.close(); } catch (IOException e) { e.printStackTrace(); } } } } class OutputStream implements Runnable { private final PipedOutputStream o; public OutputStream(PipedOutputStream o) { this.o = o; } @Override public void run() { try { Thread.sleep(5000); o.write("我来了,准备好了吗?".getBytes()); o.flush(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } finally { try { o.close(); } catch (IOException e) { e.printStackTrace(); } } } }
java多线程流
猜你喜欢
转载自blog.csdn.net/weixin_43319279/article/details/105738066
今日推荐
周排行