【问题标题】:Java pipes, why i get the write end dead exceptionJava管道,为什么我得到写结束死异常
【发布时间】:2016-02-04 01:41:07
【问题描述】:

我正在尝试使用在 2 个线程之间进行通信的管道创建一个程序(您可以说 2 个线程之间的聊天),我的问题是当您编写时没有问题,但是当您从管道中读取时,它会抛出结束死胡同的例外。我做了一个发送和接收方法,但我的接收应该知道字符串的长度是由发送方方法发送的,我在不知道发送的字符串长度的情况下做了另一个同名的接收方法。

我的代码由 3 个类组成,如下所示:

package pipes1;
import java.io.*;

public class Pipe 
{
private PipedWriter writer;
private PipedReader reader;

public PipedWriter getWriter() 
{
    return writer;
}

public PipedReader getReader() 
{
    return reader;
}

public Pipe()
{
    writer = new PipedWriter();
    reader = new PipedReader();
}
}

================================================ =========

package pipes1;

import java.io.IOException;

public class Person 
{
private String name; //name of person
private String msg1;
private String msg2;
private Pipe pipe;

public String getMsg1() 
{
    return msg1;
}

public String getMsg2() 
{
    return msg2;
}

public Pipe getPipe() 
{
    return pipe;
}

public String getName() 
{
    return name;
}

public Person(String name,Pipe pipe,String s1,String s2)
{
    this.name = name;
    this.msg1 = s1;
    this.msg2 = s2;
    this.pipe = pipe;
}

public void connection(Person x) throws Throwable
{
    pipe.getReader().connect(x.pipe.getWriter());
}

public void closing() throws IOException
{
    this.pipe.getReader().close();
    this.pipe.getWriter().close();
}

public void send(String m) throws IOException
{
    this.pipe.getWriter().write(m);
    this.pipe.getWriter().flush();
}

public void recieve() throws IOException
{
    int data = this.pipe.getReader().read();

    while(data!=-1)
    {
            System.out.print((char)data);
            data = this.pipe.getReader().read();
    }
    System.out.println("");
}

public void recieve(String m) throws IOException
{
    int i = 0;
    while(i<m.length())
    {
            System.out.print((char) this.pipe.getReader().read());
            i++;
    }
    System.out.println("");
}
}

================================================ ====================

package pipes1;

public class Main 
{
public static void main(String[] args) throws Throwable
{
    Pipe p1 = new Pipe();
    Pipe p2 = new Pipe();
    Person alice = new Person("Alice",p1,"recieved,thanks","hi bob");
    Person bob = new Person("Bob",p2,"hi alice","recieved, thanks");
    alice.connection(bob);
    bob.connection(alice);

    Thread terminal1 = new Thread(new Runnable()
    {
        @Override
        public void run() 
        {
            try 
            {
                bob.send(bob.getName()+":"+bob.getMsg1());
                bob.recieve(alice.getName()+":"+alice.getMsg1());
                bob.recieve(alice.getName()+":"+alice.getMsg2());
                bob.send(bob.getName()+":"+bob.getMsg2());
                bob.send("hi");
                bob.send("hi");
            } 
            catch (Throwable e) 
            {
                System.out.println(e.getMessage());
            }
        }
    });

    //terminal of a
    Thread terminal2 = new Thread(new Runnable()
    {
        @Override
        public void run() 
        {
            try 
            {
                alice.recieve(bob.getName()+":"+bob.getMsg1());
                alice.send(alice.getName()+":"+alice.getMsg1());
                alice.send(alice.getName()+":"+alice.getMsg2());
                alice.recieve(bob.getName()+":"+bob.getMsg2());
                alice.recieve();
                alice.recieve();
            } 
            catch (Throwable e) 
            {
                System.out.println(e.getMessage());
            }
        }
    });
    terminal1.start();
    terminal2.start();
}
}

================================================ ===================

结果是这样的:

鲍勃:嗨,爱丽丝

爱丽丝:收到,谢谢

爱丽丝:嗨,鲍勃

鲍勃:收到,谢谢

hihiWrite end dead

【问题讨论】:

  • 使用e.printStackTrace(); 而不是e.getMessage(); 来显示异常的完整堆栈跟踪。

标签: java multithreading pipe


【解决方案1】:

写入管道的线程在未关闭管道的情况下结束,离开管道broken。随后从PipedReader 读取的尝试检测到了这一点并抛出了IOException

来自方法PipedReader.read()的javadoc:

public int read()

抛出 IOException

...

投掷:

IOException - 如果管道损坏、未连接、关闭或发生 I/O 错误。

来自PipedInputStream的javadoc:

如果正在向连接的管道输出流提供数据字节的线程不再活动,则称该管道已损坏。

我认为您可以通过在第一个线程中添加bob.closing() 来避免该错误。 (我还没有测试过。)每个编写器线程都应该真正关闭它正在写入的管道。

【讨论】:

  • 我试过了,异常死了,现在我这里有一个新问题。是当我从线程 1 向线程 2 发送消息,然后从线程 2 向线程 1 发送消息时它不起作用,它看起来像是发生了死锁。
  • 好吧,我本来建议给作者打电话flush(),但你已经这样做了。好的!下一步将是使用调试器来完成。如果这没有帮助,您可以针对新问题开始一个新的 stackoverflow 问题。如果是死锁,一定要指出线程卡在哪里。
  • 好的,我会试着做一个调试,直到我卡住了,我会在这里做一个新的问题,谢谢我的java兄弟,你帮我解决了一半的问题。
猜你喜欢
  • 2011-07-24
  • 1970-01-01
  • 2018-06-22
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多