We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java IO 中的管道为运行在同一个 JVM 中的两个线程提供了通信能力。所以管道可以作为数据源以及目标媒介。
在 Java 中,通信的双方应该是运行在同意进程中的不同线程
PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); // PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream); // 管道建立连接,或者使用构造函数直接连接 pipedInputStream.connect(pipedOutputStream); new Thread(new Runnable() { @Override public void run() { try { pipedOutputStream.write("Hello World".getBytes()); pipedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { @Override public void run() { try { byte[] arr = new byte[128]; while (pipedInputStream.read(arr) != -1) { System.out.println(Arrays.toString(arr)); } pipedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }).start();
注意 当使用两个相关联的管道流时,务必将它们分配给不同的线程。read() 和 write() 方法在同一个线程中调用会导致流阻塞,这意味着如果尝试在一个线程中同事进行读和写,可能会导致线程死锁。
实际上线程在大多数情况下会传递完整的对象信息,并非原始的字节数据。但是如果要在线程之间传递字节数据,Java IO 的管道是一个不错的选择。
The text was updated successfully, but these errors were encountered:
DraperHXY
No branches or pull requests
Java IO 中的管道为运行在同一个 JVM 中的两个线程提供了通信能力。所以管道可以作为数据源以及目标媒介。
在 Java 中,通信的双方应该是运行在同意进程中的不同线程
注意 当使用两个相关联的管道流时,务必将它们分配给不同的线程。read() 和 write() 方法在同一个线程中调用会导致流阻塞,这意味着如果尝试在一个线程中同事进行读和写,可能会导致线程死锁。
实际上线程在大多数情况下会传递完整的对象信息,并非原始的字节数据。但是如果要在线程之间传递字节数据,Java IO 的管道是一个不错的选择。
The text was updated successfully, but these errors were encountered: