使用PipedInputStream Java编写结尾死异常 [英] Write end dead exception using PipedInputStream java

查看:253
本文介绍了使用PipedInputStream Java编写结尾死异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下情况下会发生写入结尾死异常: 两个线程:

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);

在这种情况下:A和B同时运行,A写入管道,而B读取管道. B刚从管道读取,此管道的缓冲区被清除.然后,A不会以未知间隔将味精写入管道.但是,在某一时刻,由于管道的缓冲区仍然为空,B再次读取了管道并发生了java.io.IOException: write end dead.而且我不想sleep()线程B等待A编写管道,这也是不稳定的.如何避免并解决这个问题?谢谢

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

推荐答案

当您具有以下条件时,将出现写入结束"异常:

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

  • 连接到PipedOutputStream的PipedInputStream和
  • 这些管道的末端由两个不同的线程读取/写入
  • 螺纹在不关闭管道侧面的情况下完成.

要解决此异常,只需在完成向管道流中的字节读写操作之后,只需在线程的可运行线程中关闭管道流即可.

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.

以下是一些示例代码:

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/

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

这篇关于使用PipedInputStream Java编写结尾死异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆