使用 PipedInputStream java 编写结束死异常

发布于 2024-10-27 07:33:57 字数 526 浏览 0 评论 0原文

以下情况会发生写端死异常: 两个线程:

A: PipedOutputStream put = new PipedOutputStream();
   String msg = "MESSAGE";
   output.wirte(msg.getBytes());
   output.flush();

B: PipedInputStream get = new PipedOutputStream(A.put);  
   byte[] get_msg = new byte[1024];
   get.read(get_msg);

情况如下:A 和 B 并发运行,A 写入管道,B 读取管道。 B 刚刚从管道中读取数据,并且该管道的缓冲区被清除。然后 A 不会以未知的时间间隔将 msg 写入管道。然而,在某一时刻,B再次读取管道时,出现了java.io.IOException: write end dead,因为管道的缓冲区仍然是空的。而且我不想让线程 B sleep() 等待 A 写入管道,这也不稳定。如何避免并解决这个问题呢?谢谢

Write end dead exception occurs in the following situation:
Two threads:

A: PipedOutputStream put = new PipedOutputStream();
   String msg = "MESSAGE";
   output.wirte(msg.getBytes());
   output.flush();

B: PipedInputStream get = new PipedOutputStream(A.put);  
   byte[] get_msg = new byte[1024];
   get.read(get_msg);

Here is the situation: A and B run concurrently, and A writes to the pipe and B reads it. B just read from the pipe and buffer of this pipe is cleared. Then A doesn't write msg to the pipe in unknown interval. However, at one moment, B read the pipe again and java.io.IOException: write end dead occurs, because the buffer of the pipe is still empty. And I don't want to sleep() thread B to wait for A writing the pipe, which is also unstable. How to avoid this problem and solve it? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-11-03 07:33:57

当您有以下情况时,将出现“写入结束死”异常:

  • PipedInputStream 连接到 PipedOutputStream 并且
  • 这些管道的末端由两个不同的线程读取/写入
  • 线程完成时没有关闭其管道的一侧。

要解决此异常,只需在完成向管道流写入字节和从管道流读取字节后,关闭线程可运行中的管道流即可。

这是一些示例代码:

final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);

    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                output.write("Hello Piped Streams!! Used for Inter Thread Communication".getBytes());                       
                output.close();

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

    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {

            try {

                int data;

                while((data = input.read()) != -1) {
                    System.out.println(data + " ===> " + (char)data);
                }

                input.close();

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

        }
    });

    thread1.start();
    thread2.start();

完整的代码在这里:https ://github.com/prabhash1785/Java/blob/master/JavaCodeSnippets/src/com/prabhash/java/io/PipedStreams.java

有关更多详细信息,请查看这个不错的博客:https://techtavern.wordpress.com/2008/07/16/whats- this-ioexception-write-end-dead/

"Write end dead" exceptions will arise when you have:

  • A PipedInputStream connected to a PipedOutputStream and
  • The ends of these pipe are read/written by two different threads
  • The threads finish without closing their side of the pipe.

To resolve this exception, simply close your Piped Stream in your Thread's runnable after you have completed writing and reading bytes to/from the pipe stream.

Here is some sample code:

final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);

    Thread thread1 = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                output.write("Hello Piped Streams!! Used for Inter Thread Communication".getBytes());                       
                output.close();

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

    Thread thread2 = new Thread(new Runnable() {
        @Override
        public void run() {

            try {

                int data;

                while((data = input.read()) != -1) {
                    System.out.println(data + " ===> " + (char)data);
                }

                input.close();

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

        }
    });

    thread1.start();
    thread2.start();

Complete code is here: https://github.com/prabhash1785/Java/blob/master/JavaCodeSnippets/src/com/prabhash/java/io/PipedStreams.java

For more details, please have a look at this nice blog: https://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

无声静候 2024-11-03 07:33:57

您需要在写入线程完成之前(当然是在写入所有数据之后)关闭PipedOutputStream。当没有写入线程且 writer 未正确关闭时,PipedInputStream 在 read() 上抛出此异常

you need to close PipedOutputStream, before writing thread is finished (and ofcourse after all data is written). PipedInputStream throws this exception on read() when there is no writing thread and writer is not properly closed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文